Rails - Form_tag for custom action

17,523
<%= form_tag(set_game_games_path) do %>
 ...
<% end %>

#routes.rb
resources :games do
  collection do
    get '/set_game', :as => :set_game
  end
end
Share:
17,523
Worker 8
Author by

Worker 8

Updated on June 11, 2022

Comments

  • Worker 8
    Worker 8 almost 2 years

    I have a controller games and a method:

    def index
    
    @games = Game.all
    
    end
    
    def set_game
    
    @current_game = Game.find(params[:set_game])
    
    end
    

    In my view I have:

    <%= form_tag("/games") do %>
    <% @games.each do |g| %>
    <%= radio_button_tag(:game_id, g.id) %>
    <%= label_tag(:game_name, g.name) %><br>
    <% end %>
    <%= submit_tag "Confirm" %>
    <% end %>
    

    Routes:

      resources :games
    
      match 'games', :to => 'game#index'
    

    How can I make this form work for my set_game method?

    Thanks.

    • awilkening
      awilkening almost 13 years
      More information is going to be needed. Also, your form is incorrectly structured. The label does not match an input.
    • Worker 8
      Worker 8 almost 13 years
      I edited with more info. If you need anything else tell me.
    • Svilen
      Svilen almost 13 years
      "How can I make this form work for my set_game method?" - it's not very clear to me what you really want to achieve and what exactly do you want to do with the set_game action. Can you please provide more information what are you trying to do?