News for hackers. Stuff that matters.
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

There are no comments on this comment yet.