김혜진
김혜진

Reputation: 315

How can I pass params using link_to?

I want to pass a parameter using link_to. (Also I am trying to use Bootstrap tab)

ilban.html.erb

<%= link_to '일반공지', '#home', { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab', :where => 1 } %>

cpu_controller.rb

@where = params[:where]

This code doesn't get where as an parameter. How can I fix it?

Upvotes: 1

Views: 7312

Answers (2)

Rahul2692
Rahul2692

Reputation: 344

In Rails 5, try this syntax for link_to

link_to 'Show', view_path(:id => view.id), { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab' }

In the place of view path you can edit with your controller path and pass the valid id that you need to link.

Or, try this syntax also to pass params

<%= link_to "Add car", {:controller => "car", :action => "add_car", :car   => car.id }%>

And add in your controller

@car = Car.find(params[:car])

Upvotes: 1

7urkm3n
7urkm3n

Reputation: 6311

I have not tested it, but it should pass the params that you want.

link_to "Search", searches_path(:where => 1, :when => "Today"), { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab' }

Controller:

@where = params[:where]

Upvotes: 3

Related Questions