user27111987
user27111987

Reputation: 1085

Rails erb get value of a text_field_tag

I have simple html.erb form like below

<table>
  <%= text_field_tag :tesintg %>
  <th><%= button_to 'Apply now', apply_product_index_path(:value => "Want Value of input in text field here") , method: :post %></th>
</table>

When "Apply now" button is pressed I want the value in the testing text_field_tag to be posted as query params as {"value" : "Value in the text field"}

How do I achieve this.

Upvotes: 1

Views: 2530

Answers (1)

Vekka
Vekka

Reputation: 141

I think the best way to do something like that is just to create form_tag

<%= form_tag apply_product_index_path, method: :post do %>
   <%= text_field_tag :teasing %>
   <%= submit_tag %>
<% end %>

This will pass to your controller hash params: { teasing: 'value passed as teasing }. You can easily use it from there with params[:teasing].

You don't need to grab value from text_field_tag and put it into button.

Also remember that if you are creating new object, very ofter preferred way is to use form_for tag which uses specific model. I'm not sure what are your intentions, so i'm not going to rewrite everything that has already beed said. You can read much more in here: http://guides.rubyonrails.org/form_helpers.html

Upvotes: 2

Related Questions