简体   繁体   English

通过link_to rails 4将参数传递给控制器​​方法

[英]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 我想将参数从text_field_tag传递到控制器,但不会通过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]. 它应该调用此方法,但不会通过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更改为:

    <%= 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. 我不确定为什么将params [:page]设置为文本字段的值。 Or am i missing something here? 还是我在这里想念什么?

In case you want to use the params[:page], set it to a variable in the action, 如果要使用params [:page],请在操作中将其设置为变量,

@page_number = params[:page]

The html should be changed to, html应该更改为

<%= 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: 我不建议为hidden字段和text字段提交相同的值:

<%= 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 . 您必须记住,您当前的表单使用remote: true ,这是Rails UJS的ajax包装器。 Thus, if you want to check what happens, you need to make sure you've removed remote: true from the form declaration. 因此,如果要检查发生了什么,则需要确保已从表单声明中删除remote: true

-- -

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: 如果要静态设置该值(IE用户无法更改),则需要查看button_to ,因为这可以通过hidden字段传递params

<%= 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). 这样做的问题是您无法更改视图中的页面(在params哈希中分配的值将是静态的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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