Warm tip: This article is reproduced from serverfault.com, please click

Access params from URL

发布于 2020-11-28 14:24:46

I'm extremely new to Rails and ruby code. I have passed a number of params to the URL from a link_to. However, when I try and access the params in a _form.html.erb I get no errors, however it doesn't print the parameter out. Any help would be appreciated.

<%= link_to teamsToPlay[i].to_s, new_fixture_path(
                :fixture => {
                :team1 => team1.id, 
                :team1name => team1name,
                :team2 => team2, 
                :team2name => team2name,
                :winner => '' }) 
%>

This code here simply gets the team ids of 2 teams with their names to create a game fixture

Gives me a URL

http://0.0.0.0:3000/fixtures/new?fixture%5Bteam1%5D=1&fixture%5Bteam1name%5D=team&fixture%5Bteam2%5D=3&fixture%5Bteam2name%5D=Arsenal&fixture%5Bwinner%5D=

So you can see the params have been passed to the URL

When I try and access these params

  <div class="field">
    <%= form.label :team2%>
    <%= form.text_field :team2 %>
    <%= params[:team2name] %>
 </div>

I have tried doing this

form.text_field :team2 , params[:team2name] 

However I didn't have any luck at all.

Please let me know if you need anymore information, cheers

Questioner
George
Viewed
0
George 2020-11-28 22:42:20

I have found a solution!

I accessed the @fixture.team1name and @fixture.team2name fields for anyone wondering...