Form submission in rails 3
I decided to start a little project in rails 3 and I am a little bit stuck on a form... Where 开发者_开发百科can I specified the f.submit action should go to a special controller / action ?
The code in the form is:
<%= form_for @user, :url => { :action => "login" } do |f| %>
<div class="field">
<%= f.text_field :email %><br />
<%= f.text_field :password %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
User is defined as @user = User.new in "index" method of "home_controller".
but I have the error:
No route matches {:controller=>"home", :action=>"login"}
as soon as I run http://0.0.0.0:3000
I am very sorry for this newbee question but I cannot find the routing details (I worked a little bit with rails a couple of years ago but...)
Thanks, Luc
You don't need to specify any action for f.sumbit. First of all, you need to make sure you put
resources :users
(for example)
in your routes.rb
then if you want to create a user
put
def new
@user = User.new
end
in your users_controller so you have a page to create new user or you can put @user=User.new anywhere you like, remember to set the route correctly
then
def create
@user = User.new(params[:id])
if @user.save
sign_in @user
redirect_to @user
else
render 'new'
end
end
is the part that does real work after you hit on submit
the actual part that connect your form with everything else is this line
<% form_for @user do |f| %>
you can change user to other object, and you can also edit form using update action in a controller.
Hope you got the idea
Whenever you use REST objects, the mere:
form_for @article
is enough for the form to find the proper path.
Otherwise, you can use helpers this way:
form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form")
More info here: http://edgeguides.rubyonrails.org/form_helpers.html
精彩评论