ruby - No route matches [POST] "/sessions/new" (rails 4 in action) -
i'm next rails 4 in action book , i'm running next error:
no route matches [post] "/sessions/new"
i'm not sure why, i've followed tutorial t. leaves me believe tutorial flawed. anyhow, reason it's posting sessions#new when should posting sessions#create. doing wrong?
sessions_controller.rb
class sessionscontroller < applicationcontroller def new end def create user = user.where(:name => params[:signin][:name]).first if user && user.authenticate(params[:signin][:password]) session[:user_id] = user.id flash[:notice] = "signed in successfully." redirect_to root_url else flash[:error] = "sorry." render :new end end end
sessions/new.html.erb
<h1>sign in</h1> <%= form_for :signin, method: "post" |f| %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <p> <%= f.label :password %><br /> <%= f.password_field :password %> </p> <%= f.submit "sign in" %> <% end %>
routes.rb
"/signin", to: "sessions#new" post "/signin", to: "sessions#create"
you right there error in book.
when have form tag
<%= form_for :signin, method: "post" |f| %>
if rails doesn't know :signin
means, re-create current url , submit form url (in case 'sessions/new'), using post verb (whether specify or not!)
<form accept-charset="utf-8" action="/sessions/new" method="post"> ....
obviously, that's not want because don't have /sessions/new post http verb, think easiest solution specify route
post "/signin", to: "sessions#create", as: "signin"
and in form_for
<%= form_for signin_path |f| %>
ruby-on-rails ruby ruby-on-rails-4
No comments:
Post a Comment