简体   繁体   中英

pass params to controller method via link_to rails 4

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 %>

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" %>

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'%>

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM