I am passing several params from my view but need to put some logic behind one of them. I pass a value of either "Yes" or "No" from my view to the controller with the param known as "completed" under "update". Here is my controller and view which works fine: view-> <%= form_tag :action => ''update'', :id => @project %> <% form_for :project do |f| -%> <table> <tr> <td><label for= "name">Project Name</label> <%= f.text_field :name %> </td><td> <%= f.label :completed %> <%= f.select(:completed, ["No", "Yes"] )%> </td></tr></table> <%= submit_tag ''Submit'' %> <%= form_tag %> <% end -%> controller-> def update @project = Project.find(params[:id]) if @project.update_attributes(params[:project]) redirect_to :action => ''index'', :id => @project else render :action => ''edit'' end end I would like to add some logic to do something if the "completed" param is equal to "Yes". Here is my attempt but it doesn''t work: def update @project = Project.find(params[:id]) if @project.update_attributes(params[:project]) if params[:completed] == "Yes" do "something here" end redirect_to :action => ''index'', :id => @project else render :action => ''edit'' end end Can anyone help me put some logic in for just the value of the completed param? thanks -- 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.
On 8 March 2011 16:07, A. Mcbomb <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I am passing several params from my view but need to put some logic > behind one of them. I pass a value of either "Yes" or "No" from my view > to the controller with the param known as "completed" under "update". > > Here is my controller and view which works fine: > > view-> > > <%= form_tag :action => ''update'', :id => @project %> > <% form_for :project do |f| -%> > <table> > <tr> > <td><label for= "name">Project Name</label> > <%= f.text_field :name %> > </td><td> > <%= f.label :completed %> > <%= f.select(:completed, ["No", "Yes"] )%> > </td></tr></table> > <%= submit_tag ''Submit'' %> > <%= form_tag %> > <% end -%> > > controller-> > > def update > @project = Project.find(params[:id]) > if @project.update_attributes(params[:project]) > redirect_to :action => ''index'', :id => @project > else > render :action => ''edit'' > end > end > > I would like to add some logic to do something if the "completed" param > is equal to "Yes". Here is my attempt but it doesn''t work: > > def update > @project = Project.find(params[:id]) > if @project.update_attributes(params[:project]) > if params[:completed] == "Yes" do "something here" endWhen you say it does not work what do you mean? If it is just a syntax error that is because you have not got the syntax for ''if'' right, it should be if params[:completed] == "Yes" ..... end Note however that you are doing it *after* the record is saved, which happens in update_attributes. If you are trying to do it before the save then move it up to before the update. Colin -- 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.
Hi, You can remove the form_tag. form_for will create that for you. Then use something like: f.label :completed f.select(:completed, [[''Yes'', true],[''No'', false]]) Presumably Project has a boolean field called ''completed'' with probably a :default => false on the migration. Once you''ve updated your attributes you should probably just use: if @project.completed? # do something clever here... end On 8 March 2011 16:07, A. Mcbomb <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I am passing several params from my view but need to put some logic > behind one of them. I pass a value of either "Yes" or "No" from my view > to the controller with the param known as "completed" under "update". > > Here is my controller and view which works fine: > > view-> > > <%= form_tag :action => ''update'', :id => @project %> > <% form_for :project do |f| -%> > <table> > <tr> > <td><label for= "name">Project Name</label> > <%= f.text_field :name %> > </td><td> > <%= f.label :completed %> > <%= f.select(:completed, ["No", "Yes"] )%> > </td></tr></table> > <%= submit_tag ''Submit'' %> > <%= form_tag %> > <% end -%> > > controller-> > > def update > @project = Project.find(params[:id]) > if @project.update_attributes(params[:project]) > redirect_to :action => ''index'', :id => @project > else > render :action => ''edit'' > end > end > > I would like to add some logic to do something if the "completed" param > is equal to "Yes". Here is my attempt but it doesn''t work: > > def update > @project = Project.find(params[:id]) > if @project.update_attributes(params[:project]) > if params[:completed] == "Yes" do "something here" end > redirect_to :action => ''index'', :id => @project > else > render :action => ''edit'' > end > end > > Can anyone help me put some logic in for just the value of the completed > param? > > thanks > > -- > 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. > >-- Tim Harding Well Informed Ltd Registered in England & Wales Company number 06707839 Registered office: Suite 235, 77 Beak St, London, W1F 9DB -- 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.
On Tue, Mar 8, 2011 at 12:07 PM, A. Mcbomb <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I am passing several params from my view but need to put some logic > behind one of them. I pass a value of either "Yes" or "No" from my view > to the controller with the param known as "completed" under "update". > > Here is my controller and view which works fine: > > view-> > > <%= form_tag :action => ''update'', :id => @project %> > <% form_for :project do |f| -%> > <table> > <tr> > <td><label for= "name">Project Name</label> > <%= f.text_field :name %> > </td><td> > <%= f.label :completed %> > <%= f.select(:completed, ["No", "Yes"] )%> > </td></tr></table> > <%= submit_tag ''Submit'' %> > <%= form_tag %> > <% end -%> > > controller-> > > def update > @project = Project.find(params[:id]) > if @project.update_attributes(params[:project]) > redirect_to :action => ''index'', :id => @project > else > render :action => ''edit'' > end > end > > I would like to add some logic to do something if the "completed" param > is equal to "Yes". Here is my attempt but it doesn''t work: > > def update > @project = Project.find(params[:id]) > if @project.update_attributes(params[:project]) > if params[:completed] == "Yes" do "something here" end > redirect_to :action => ''index'', :id => @project > else > render :action => ''edit'' > end > end > > Can anyone help me put some logic in for just the value of the completed > param? > >you have a form inside another form. change it to <% form_for @project do |f| -%> <table> <tr> <td><label for= "name">Project Name</label> <%= f.text_field :name %> </td><td> <%= f.label :completed %> <%= f.select(:completed, ["No", "Yes"] )%> </td></tr></table> <%= submit_tag ''Submit'' %> <%= form_tag %> <% end -%> catch what you want like this params[:project][:completed] -- 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.
thanks alot for all the help! -- 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.