S Ahmed
2011-May-05 17:35 UTC
form_for, need to modify the :url based on if it is persisted or not
My have a form in a partial like this: form_for @article, :url => articles_path, ..... Now currently it posts to the create action. I want it to post to comment_article_path under certain conditions, how can I do this? Can I put in a expression like: @article.persisted? comment_article_path : articles_path Is this good practice? -- 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.
Tim Shaffer
2011-May-05 19:56 UTC
Re: form_for, need to modify the :url based on if it is persisted or not
It can certainly be done easy enough, but I''d question the problem you are trying to solve if you want to do something like that. -- 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.
Eugen Ciur
2011-May-05 20:02 UTC
Re: form_for, need to modify the :url based on if it is persisted or not
You can create a helper method (in helpers/articles_helper.rb): def correct_post_path @article.persisted? comment_article_path : articles_path end And then use this helper in your form builder form_for @article, correct_post_path do ... end That helps ? Maybe you can tell us under what conditions you need to change post url, maybe you over-engineer somewhere... ---- http://blog.eugen.co -- 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.
S Ahmed
2011-May-07 01:04 UTC
Re: Re: form_for, need to modify the :url based on if it is persisted or not
Thanks. When you add something to a helper like articles_helper, does it have the request object in there also? How does it get the @articles object when I call it like: form_for @article, correct_post_path, ... I would have though it would be: form_for @article, correct_post_path(@article), ... On Thu, May 5, 2011 at 4:02 PM, Eugen Ciur <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> You can create a helper method (in helpers/articles_helper.rb): > def correct_post_path > @article.persisted? comment_article_path : articles_path > end > > And then use this helper in your form builder > form_for @article, correct_post_path do ... end > > That helps ? Maybe you can tell us under what conditions you need to > change post url, maybe you over-engineer somewhere... > > ---- > http://blog.eugen.co > > -- > 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. > >-- 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.
Frederick Cheung
2011-May-07 19:04 UTC
Re: Re: form_for, need to modify the :url based on if it is persisted or not
On 7 May 2011, at 02:04, S Ahmed <sahmed1020-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks. > > When you add something to a helper like articles_helper, does it have the request object in there also? >> How does it get the @articles object when I call it like: >Controller instance variables are copied across to the view object so that just works (although you can of course pass parameters into your helpers explicitly) Fred> form_for @article, correct_post_path, ... > > I would have though it would be: > > form_for @article, correct_post_path(@article), ... > > > > > On Thu, May 5, 2011 at 4:02 PM, Eugen Ciur <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > You can create a helper method (in helpers/articles_helper.rb): > def correct_post_path > @article.persisted? comment_article_path : articles_path > end > > And then use this helper in your form builder > form_for @article, correct_post_path do ... end > > That helps ? Maybe you can tell us under what conditions you need to > change post url, maybe you over-engineer somewhere... > > ---- > http://blog.eugen.co > > -- > 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. > > > -- > 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.-- 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.