News for hackers. Stuff that matters.
Re:Offer HN: Help with Ruby on Rails by Robin_Message on Wednesday October 27, 2010 @12:24PM (#1839258)
Thanks, but I already ruled out both of these solutions.

1. I know you can look at the value of the submitted button on the server. What you need to use is actually <input type=submit name=some_non_clashing_string_that_identifies_this_button value=ButtonText>, so my question is how do you generate those strings nicely in Rails and have the controller act on them nicely as well.

2. Of course that's what you need to do, as I said in my question. My question was, how do I make Rails do this in a nice way? fields_for helps, but I'd like to know if there is a plugin or something better.

Re:Offer HN: Help with Ruby on Rails by erikpukinskis on Wednesday October 27, 2010 @09:23PM (#1841254)
In your view you can just do this:

    - form_for concertgoer do |f|
      = f.submit "Submit", :name => "music[jazz]"
      = f.submit "Submit", :name => "music[classical]"
That's HAML... let me know if you want to see the ERB. If you want to abstract even further you can do:

    # In app/helpers/concertgoer_helper.rb
    def music_submit(form, genre)
      form.submit "Submit", :name => "music[#{genre}]"
    end

    # In app/views/concertgoer/get_ready.html.haml
    - form_for concertgoer do |f|
      = music_submit(f, "jazz")
      = music_submit(f, "classical")
     
Then in your controller you can just do:

    music = params[:music].keys.first
That variable will either be "jazz" or "classical" depending on which button was clicked. If you want to have different behaviors you can use a case statement:

    case params[:music].keys.first
      when "jazz" then concertgoer.put_on_turtleneck
      when "classical" then concertgoer.pour_glass_of_wine
    end