如何在Rails中获取URL参数?

时间:2021-09-24 19:41:53

When a POST request is processed, params[:id] returns the value of "id" stored in the form that was posted.

处理POST请求时,params [:id]返回存储在已发布表单中的“id”值。

However I want to get the value stored in the url (query string).

但是我想获取存储在url(查询字符串)中的值。

def some_action
  id = params[:id] # Gets id from here: /some_action?id=[VALUE] only when request.post? is false
  if request.post?
  # Do some stuff, can't get the id value from the url
  ...
end

So how to get the value from the URL in a POST request?

那么如何从POST请求中获取URL的值?

1 个解决方案

#1


14  

it's the same actually as GET parameters. To prove that, I have a search form that posts one parameter to the same action :

它与GET参数实际上相同。为了证明这一点,我有一个搜索表单,将一个参数发布到同一个操作:

def invite

    if request.post? 

        search= [params[:search]]
        #...

    end
end

And in the view - maybe your problem comes from this

在视图中 - 也许你的问题来自于此

<% form_tag do %>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Rechercher" %>
<% end %>

#1


14  

it's the same actually as GET parameters. To prove that, I have a search form that posts one parameter to the same action :

它与GET参数实际上相同。为了证明这一点,我有一个搜索表单,将一个参数发布到同一个操作:

def invite

    if request.post? 

        search= [params[:search]]
        #...

    end
end

And in the view - maybe your problem comes from this

在视图中 - 也许你的问题来自于此

<% form_tag do %>
        <%= text_field_tag :search, params[:search] %>
        <%= submit_tag "Rechercher" %>
<% end %>