Ruby-on-rails 如何在 Rails 中获取 URL 参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4058657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:30:59  来源:igfitidea点击:

How to get URL parameters in Rails?

ruby-on-railsurlpost

提问by Alex

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 中获取值呢?

回答by Marcel Falliere

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