Is there a way in rails to choose controller action to which form will be submitted on the client side? I have a small web album and display multiple images on a page. Each image has associated check box allowing user to select many images. User selects some images and chose action: rotate left, right, flip, delete, ... So I need to post a form with selected check boxes, but the action depends on the user choice. Currently I have something like this: VIEW: <%= start_form_tag({:action => ''action_chooser'', :id => @id}, {''name'' => frm}) %> ... <%= hidden_field_tag("action_type") %> <%= link_to_function "action1", "document.frm.action_type.value=''action1''; document.frm.submit()" %> <%= link_to_function "action2", "document.frm.action_type.value=''action2''; document.frm.submit()" %> CONTROLLER: def action_chooser case action_type when ''action1'' ... when ''action2'' I would like to have action1 and action2 functions in controller and somehow choose on the client where to submit. I really don''t like this case statement. Sorry for reposting the same question (as few days before), but I have feeling that there must be more elegant solution. Thanks, Igor.
You don''t need the case statement if you have seperate forms for each action - then you''ll have a more modular testable controller. sam On 5/11/05, ianic <ianic-eC0b4XhvfLY@public.gmane.org> wrote:> Is there a way in rails to choose controller action to which form will > be submitted on the client side? > > I have a small web album and display multiple images on a page. Each > image has associated check box allowing user to select many images. > > User selects some images and chose action: rotate left, right, flip, > delete, ... > So I need to post a form with selected check boxes, but the action > depends on the user choice. > > Currently I have something like this: > > VIEW: > <%= start_form_tag({:action => ''action_chooser'', :id => @id}, {''name'' => > frm}) %> > ... > <%= hidden_field_tag("action_type") %> > <%= link_to_function "action1", > "document.frm.action_type.value=''action1''; document.frm.submit()" %> > <%= link_to_function "action2", > "document.frm.action_type.value=''action2''; document.frm.submit()" %> > > CONTROLLER: > def action_chooser > case action_type > when ''action1'' > ... > when ''action2'' > > I would like to have action1 and action2 functions in controller and > somehow choose on the client where to submit. I really don''t like this > case statement. > > Sorry for reposting the same question (as few days before), but I have > feeling that there must be more elegant solution. > > Thanks, > Igor. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- sam http://www.magpiebrain.com/
I need to change my question a little. For an input control on the html form, is there a way to post that control to the different controller actions depending on the user selection? Thanks, Igor. Date: Wed, 11 May 2005 10:34:01 +0100 From: Sam Newman <sam.newman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> Subject: Re: [Rails] choosing controller action on the client To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Message-ID: <b5d1675a050511023462841230-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org> Content-Type: text/plain; charset=ISO-8859-1 You don''t need the case statement if you have seperate forms for each action - then you''ll have a more modular testable controller. sam On 5/11/05, ianic <ianic-eC0b4XhvfLY@public.gmane.org> wrote: >> Is there a way in rails to choose controller action to which form will >> be submitted on the client side? >> >> I have a small web album and display multiple images on a page. Each >> image has associated check box allowing user to select many images. >> >> User selects some images and chose action: rotate left, right, flip, >> delete, ... >> So I need to post a form with selected check boxes, but the action >> depends on the user choice. >> >> Currently I have something like this: >> >> VIEW: >> <%= start_form_tag({:action => ''action_chooser'', :id => @id}, {''name'' => >> frm}) %> >> ... >> <%= hidden_field_tag("action_type") %> >> <%= link_to_function "action1", >> "document.frm.action_type.value=''action1''; document.frm.submit()" %> >> <%= link_to_function "action2", >> "document.frm.action_type.value=''action2''; document.frm.submit()" %> >> >> CONTROLLER: >> def action_chooser >> case action_type >> when ''action1'' >> ... >> when ''action2'' >> >> I would like to have action1 and action2 functions in controller and >> somehow choose on the client where to submit. I really don''t like this >> case statement. >> >> Sorry for reposting the same question (as few days before), but I have >> feeling that there must be more elegant solution. >> >> Thanks, >> Igor. >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> --
All you want to do is dynamically call your method based upon what you get back. Setup a method within your controller that corresponds to each possible return value from the form and then call them using object.send Have a look in the "Calling Methods Dynamically" section of the following page in the pickaxe http://www.ruby-doc.org/docs/ProgrammingRuby/ That''s the ruby way of dealing with it as far as I understand..... On 5/11/05, ianic <ianic-eC0b4XhvfLY@public.gmane.org> wrote:> Is there a way in rails to choose controller action to which form will > be submitted on the client side? >-- John W Higgins wishdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
In short - no. There is a one-to-one mapping between a form post and a Rails controller method to handle the post. As I see it there are three options: 1. Use multiple forms - my favorite (for reasons previously mentioned) 2. Use a single form and single controller, and have the controller''s method call some other method based on some aspect of the form parameter (e.g. the value of a select) 3. Post to different locations (controllers) dynamically using Javascript - e.g. you click a button, which calls some Javascript, which submits a post to your controller. This is nasty - Javascript is harder to maintain (develop, test) than Ruby. If you really only want one form, go with option two - although I''m unsure why having multiple forms is not acceptable - some of my table views have 2 or more forms per row for different actions (e.g. Go to Item, Delete Item) and it works quite well. sam On 5/11/05, ianic <ianic-eC0b4XhvfLY@public.gmane.org> wrote:> I need to change my question a little. > > For an input control on the html form, is there a way to post that > control to the different controller actions depending on the user selection? > > Thanks, > Igor. > > Date: Wed, 11 May 2005 10:34:01 +0100 > From: Sam Newman <sam.newman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > Subject: Re: [Rails] choosing controller action on the client > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Message-ID: <b5d1675a050511023462841230-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org> > Content-Type: text/plain; charset=ISO-8859-1 > > You don''t need the case statement if you have seperate forms for each > action - then you''ll have a more modular testable controller. > > sam > > On 5/11/05, ianic <ianic-eC0b4XhvfLY@public.gmane.org> wrote: > > >> Is there a way in rails to choose controller action to which form will > >> be submitted on the client side? > >> > >> I have a small web album and display multiple images on a page. Each > >> image has associated check box allowing user to select many images. > >> > >> User selects some images and chose action: rotate left, right, flip, > >> delete, ... > >> So I need to post a form with selected check boxes, but the action > >> depends on the user choice. > >> > >> Currently I have something like this: > >> > >> VIEW: > >> <%= start_form_tag({:action => ''action_chooser'', :id => @id}, {''name'' => > >> frm}) %> > >> ... > >> <%= hidden_field_tag("action_type") %> > >> <%= link_to_function "action1", > >> "document.frm.action_type.value=''action1''; document.frm.submit()" %> > >> <%= link_to_function "action2", > >> "document.frm.action_type.value=''action2''; document.frm.submit()" %> > >> > >> CONTROLLER: > >> def action_chooser > >> case action_type > >> when ''action1'' > >> ... > >> when ''action2'' > >> > >> I would like to have action1 and action2 functions in controller and > >> somehow choose on the client where to submit. I really don''t like this > >> case statement. > >> > >> Sorry for reposting the same question (as few days before), but I have > >> feeling that there must be more elegant solution. > >> > >> Thanks, > >> Igor. > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > -- > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- sam http://www.magpiebrain.com/
I am currently running Postgresql , Rails and the production enviroment , FREEBSD running FASTCGI , on apache1.3 I am experiencing random Rails Application errors . However when running ''development'' these errors don''t pop up . I was wondering if anyone out there are having the same issues , or can think of an compatibility issues thankz niq
Sam Newman <sam.newman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:> If you really only want one form, go with option two - although I''m > unsure why having multiple forms is not acceptable - some of my table > views have 2 or more forms per row for different actions (e.g. Go to > Item, Delete Item) and it works quite well.I understand his thinking. He''s got multiple check boxes scattered around his page that he would need to have be part of each of the multiple forms. In other words, "select all the photos you want to manipulate and then click the submit button that corresponds to the action you want to perform on them." As I see it, the best method would be to have a "dispatcher" action that in turn called another method based on the submit value. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
What do your logs say? On 5/11/05, Elf <elf-+PFDhcP3fwAH3ZhH7Ptzkg@public.gmane.org> wrote:> I am currently running Postgresql , Rails and the production enviroment , > FREEBSD running FASTCGI , on apache1.3 > > I am experiencing random Rails Application errors . However when running > ''development'' these errors don''t pop up . I was wondering if anyone out > there are having the same issues , or can think of an compatibility issues > thankz > niq > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >