0

I want to pass a params from a text_field_tag to my controller but it doesnt get passed via link_to

      <div class="row" id="page-search">
        <%= hidden_field_tag :page, params[:page], class: 'form-control', value: '1'%>
        <%= text_field_tag :page, params[:page], class: 'form-control', id: 'show-page'%>
        <%= link_to 'Go', currencies_path, :remote => true, method: :get, class: "btn btn-default", id: "show-button" %>
      </div>

controller method

      def index
        @currencies = Currency.all.page(params[:page]).per(10)
      end

It should call this method which it does but it doesnt pass the params[:page]. Any clue as to how to solve this issue?

Thanks

Based on suggestions, i did this but i am still facing the same issue as it doesnt pass the params

       <%= form_tag currencies_path, :method => :get do -%>
          <%= text_field_tag :page, params[:page], class: 'form-control', id: 'show-page'%>
          <%= hidden_field_tag :page, params[:page], class: 'form-control', value: '1'%>
          <%= submit_tag 'Go' %>
        <% end %>
Kingsley Simon
  • 2,090
  • 5
  • 38
  • 84
  • Possible duplicate of [pass parameter by link\_to ruby on rails](http://stackoverflow.com/questions/1898737/pass-parameter-by-link-to-ruby-on-rails) – Bijendra Feb 02 '16 at 12:22
  • @Bijendra its a different question. urs is based on passing object data. mine is based on pasing text_field_tag input – Kingsley Simon Feb 02 '16 at 12:29

3 Answers3

1

Links are self-contained: they only care about what is in their own arguments.

If you want to submit params from fields on the page, then you need to wrap those fields in a form and submit the form.

If you want to put that data into the link then you can change the link_to thus:

    <%= link_to 'Go', currencies_path(page => 1), :remote => true, method: :get, class: "btn btn-default", id: "show-button" %>
Max Williams
  • 32,435
  • 31
  • 130
  • 197
1

This will send the value that is input as part of the text field tag.

<%= form_tag currencies_path, :method => :get do %>
      <%= text_field_tag :page, nil, class: 'form-control', id: 'show-page'%>
      <%= submit_tag 'Go' %>
<% end %>

I am not sure why you set params[:page] as a value for text-field. Or am i missing something here?

In case you want to use the params[:page], set it to a variable in the action,

@page_number = params[:page]

The html should be changed to,

<%= text_field_tag :page, @page_number, class: 'form-control', id: 'show-page'%>
Prashanth
  • 241
  • 1
  • 7
0

I don't advocate submitting the same value for both hidden and text fields:

<%= form_tag currencies_path, method: :get do %>
   <%= text_field_tag :page, params[:page], class: 'form-control', id: 'show-page'
   <%= submit_tag "Go" %>
<% end %>

You must remember, your current form uses remote: true, which is the ajax wrapper for Rails UJS. Thus, if you want to check what happens, you need to make sure you've removed remote: true from the form declaration.

--

If you wanted to set the value statically (IE the user cannot change), you'll want to look at button_to, as this can pass params through a hidden field:

<%= button_to "Go", currencies_path, method: :get, params: { page: "1" } %>

The problem with this is that you cannot change the page in the view (the value you assign in the params hash will be static).

Richard Peck
  • 76,116
  • 9
  • 93
  • 147