Hi I am having trouble with a submit code for a form. I don''t get errors or anything. it just goes to the redirect and it dosen''t show anything def create @comment = ForumComment.new(params[:post]) @comment.user_id = current_user.id if @comment.save redirect_to :controller => ''forums'', :action => ''index'' end redirect_to :action => ''create'' end end the create.rhtml just has this code for error reporting(to help me fix it) <%= error_messages_for ''forum_comment'' %><br/> is there anything wrong with the create method syntax? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi I am having trouble with a submit code for a form. I don''t get errors or anything. it just goes to the redirect and it dosen''t show anything def create @comment = ForumComment.new(params[:post]) @comment.user_id = current_user.id if @comment.save redirect_to :controller => ''forums'', :action => ''index'' end redirect_to :action => ''create'' end end the create.rhtml just has this code for error reporting(to help me fix it) <%= error_messages_for ''forum_comment'' %><br/> is there anything wrong with the create method syntax? thanks in advaced --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You probably meant that second redirect to be a render. Your code would fail if the save actually happened since you would then both render and redirect. Typically the pattern is def new @foo = Foo.new end def create @foo = Foo.new(params[:foo]) if @foo.save redirect_to :controller => ''somewhere'' else render :action = ''new'' end end Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andresmax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-31 16:40 UTC
Re: create post method error
why do you do the @foo=Foo.new twice? I understand the second once, since you are doing the create and using the params to receive it, but isn''t the other one not needed? thanks, andres On Oct 30, 4:33 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You probably meant that second redirect to be a render. Your code > would fail if the save actually happened since you would then both > render and redirect. > > Typically the pattern is > > def new > @foo = Foo.new > end > > def create > @foo = Foo.new(params[:foo]) > if @foo.save > redirect_to :controller => ''somewhere'' > else > render :action = ''new'' > end > end > > Fred--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 31 Oct 2007, at 16:40, andresmax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > why do you do the @foo=Foo.new twice? I understand the second once, > since you are doing the create and using the params to receive it, but > isn''t the other one not needed? >The first one is for the new page, which still needs an instance of foo to render the form (so everything will be blank). You could also set defaults. Fred> thanks, > > andres > > On Oct 30, 4:33 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> You probably meant that second redirect to be a render. Your code >> would fail if the save actually happened since you would then both >> render and redirect. >> >> Typically the pattern is >> >> def new >> @foo = Foo.new >> end >> >> def create >> @foo = Foo.new(params[:foo]) >> if @foo.save >> redirect_to :controller => ''somewhere'' >> else >> render :action = ''new'' >> end >> end >> >> Fred > > > --~--~---------~--~----~------------~-------~--~----~ > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > -~----------~----~----~----~------~----~------~--~--- >