Hi, I have a simple form in my controller "before_home". In the form I want to create a user, si I use the users_path on my form. But look at the html generated... Why do I have an action before_home? I should be "users" in the action. Does someone knows where this could come from? View: <% form_for users_path do |f| %> <p> <%= f.label :email %> <%= f.text_field :email %> <%= f.label :ville %> <%= text_field_with_auto_complete :user, :ville_name, { :size => 15 }, { :url => formatted_villes_path(:js), :method => :get, :param_name => ''search'' } %> <%= f.submit "Suivant" %> </p> <%end%> Generated HTML: <form method="post" action="/before_home"> <input type="submit" value="Suivant" name="commit" id="/before_home_submit"> </form> Greg -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, form_for expects the first parameter to be a new user object and if that isn''t supplied then the form action is set to the action that rendered the page; this is why you see action="/before_home". The reason user_path isn''t working is because it doesn''t reference a user object. If you wish to have the form submitted to /user then set up the user object and specify the url. See below... To create a user I suggest you create a new user object and store it in an instance variable: @user = User.new Then in the form_for method you can use that value: <% form_for :user, :url => { :controller => users_path } do |f| %> Hope this helps On 5 May, 12:03, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > I have a simple form in my controller "before_home". In the form I want > to create a user, si I use the users_path on my form. > But look at the html generated... Why do I have an action before_home? I > should be "users" in the action. > Does someone knows where this could come from? > > View: > <% form_for users_path do |f| %> > > <p> > <%= f.label :email %> > <%= f.text_field :email %> > <%= f.label :ville %> > <%= text_field_with_auto_complete :user, :ville_name, { :size => > 15 }, { :url => formatted_villes_path(:js), :method => :get, :param_name > => ''search'' } %> > > <%= f.submit "Suivant" %> > </p> > <%end%> > > Generated HTML: > <form method="post" action="/before_home"> > <input type="submit" value="Suivant" name="commit" > id="/before_home_submit"> > > </form> > > Greg > -- > Posted viahttp://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Floyd Joseph wrote:> Hi, > > form_for expects the first parameter to be a new user object and if > that isn''t supplied then the form action is set to the action that > rendered the page; this is why you see action="/before_home". The > reason user_path isn''t working is because it doesn''t reference a user > object. If you wish to have the form submitted to /user then set up > the user object and specify the url. See below... > > To create a user I suggest you create a new user object and store it > in an instance variable: > > @user = User.new > > Then in the form_for method you can use that value: > > <% form_for :user, :url => { :controller => users_path } do |f| %> > > Hope this helpsThanks for your help Floyd, it works now. However, I have another issue. How do I display the errors regarding that my form is in a different controller. if @user.save flash[:notice] = "Successfully created user." redirect_to root_path else render :action => ''index'', :controller => "before_home" end -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
In response to your second question, you would not be able to redisplay the form with errors by using render because the action is in another controller. However you can render the template that created the form: if @user.save flash[:notice] = "Successfully created user." redirect_to root_path else render :template => "before_home/new" # I am assuming new was the action in before_home end This will redisplay the form along with the errors. However, you should consider your application design, it would be easier to control the creating and updating of records under the controller that is related to a specific model. Consider implementing a more RESTful application, contact for more info or if you have further problems. On 5 May, 20:52, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Floyd Joseph wrote: > > Hi, > > > form_for expects the first parameter to be a new user object and if > > that isn''t supplied then the form action is set to the action that > > rendered the page; this is why you see action="/before_home". The > > reason user_path isn''t working is because it doesn''t reference a user > > object. If you wish to have the form submitted to /user then set up > > the user object and specify the url. See below... > > > To create a user I suggest you create a new user object and store it > > in an instance variable: > > > @user = User.new > > > Then in the form_for method you can use that value: > > > <% form_for :user, :url => { :controller => users_path } do |f| %> > > > Hope this helps > > Thanks for your help Floyd, it works now. > However, I have another issue. How do I display the errors regarding > that my form is in a different controller. > > if @user.save > flash[:notice] = "Successfully created user." > redirect_to root_path > else > render :action => ''index'', :controller => "before_home" > end > -- > Posted viahttp://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.