2

I try get 2 players from a list that i created, all I need is to get all the informations about those 2 players (name, descrption etc...) and once i have selected both, I need to be redirected to /fight path. My form doesnt work and I don't understand why.

I have the error encountered a syntax error while rendering template on line <%= form_tag ("/fight", :method => "get") do %>

Could someone help me please ?

Here is my view :

<%= form_tag ("/fight", :method => "get") do %>
  <%= label_tag :player1 %>
  <%= select_field :character, @characters.collect{|u| [u.name, u.id]} %>
  <%= label_tag :playe2 %>
  <%= select_field :character, @characters.collect{|u| [u.name, u.id]} %>
  <%= submit_tag 'Fight' %>
<% end %>

My pages_controller

def index 
  @characters = Character.all
end

And my routes

get 'fight' => 'pages#index'
ray
  • 5,454
  • 1
  • 18
  • 40
Damien Compère
  • 219
  • 1
  • 3
  • 16

1 Answers1

2

Error occured due to space provided between method form_tag and arguments provided in brackets, it should be like,

form_tag("/fight", :method => "get")

You can also write in following manners,

<%= form_tag fight_path, method: "get" do %>

or

<%= form_tag url: "fight", method: "get" do %>
ray
  • 5,454
  • 1
  • 18
  • 40
  • 2
    That may be a "better" way to write it, but it should get the same result?... I don't understand OP's error, and I don't know why this would solve it. – Tom Lord Oct 14 '19 at 14:35
  • Why can't you put a space between the method call and arguments ? This seems weird – Viktor Oct 14 '19 at 14:39
  • @Виктор This is basic you should know, read [here](https://stackoverflow.com/a/16533154/10522579) – ray Oct 14 '19 at 14:43
  • thanks for your answer, i changed the line but now i got the error "undefined method `select_field' ", should I use select_tag instead of select_field ? – Damien Compère Oct 14 '19 at 14:44
  • @ray Well now I do :) [here's](https://stackoverflow.com/questions/26480823/why-does-white-space-affect-ruby-function-calls) another explanation on the issue if anyone else didn't know this. – Viktor Oct 14 '19 at 14:59
  • Thanks ! Here is my final code "select_tag 'character_id', options_for_select(@characters.collect{ |u| [u.name, u.id] })" and the display is OK ! – Damien Compère Oct 14 '19 at 15:02