Yogzzz
Yogzzz

Reputation: 2785

Pass variable via link_to action

My view:

<div class = "btn btn-link">
  <%= link_to 'Generate Rating Set', {
    :controller => "co_view_rating", 
    :action => "generate_rating_set_co_view" } %>
</div>

This links to a action in my controller which creates a new record. How can I pass a variable via this method in my view to the action in my controller. The variable would be user input via a text field.

Upvotes: 1

Views: 1145

Answers (3)

jahrichie
jahrichie

Reputation: 1235

Adapting what @Sparda said, use a field with post, and pass a hidden_field (if you want it to be hidden) or a input_tag, or text_field

Upvotes: 0

Raindal
Raindal

Reputation: 3237

You should send your variable through a form with the POST method.

<%= form_tag('/co_view_rating/generate_rating_set_co_view') do %>
    <%= text_field_tag 'my_variable' %>
    <div class="btn btn-link">
        <%= submit_tag "Generate Rating Set" %>
    </div>
<% end %>

And in your controller action :

def generate_rating_set_co_view
    my_variable = params[:my_variable]
end

you can find more documentation on form_tags and input types here : http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

Upvotes: 1

user1454117
user1454117

Reputation:

You probably want a form if the user input comes from a text field.

<%= form_tag {:controller => "co_view_rating", :action => "generate_rating_set_co_view"}, :method => :get do |f| %>
  <%= text_field_tag :awesome_text_field, "default value" %>
  <div class="btn btn-link">
    <%= submit_tag "Generate Rating Set" %>
  </div>
<% end %>

The text input will then be available in the params hash as params[:awesome_text_field].

Upvotes: 0

Related Questions