开发者

Rails - Form_tag for custom action

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.


<%= form_tag(set_game_games_path) do %>
 ...
<% end %>

#routes.rb
resources :games do
  collection do
    get '/set_game', :as => :set_game
  end
end


That's an example of a custom route:

  match "customroute" => "controller#action", :as => "customroutename"

This can be then accessed with "customroutename_url" in your views. If you want to create a custom route for your set_game action, for example, it would be

  match "setgame" => "games#set_game", :as => "setgame"

Then you can do

<%= form_tag setgame_url %>
...
<% end %>

You can read more about custom routes here: http://guides.rubyonrails.org/routing.html#non-resourceful-routes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜