如何在Rails中获取URL参数? [英] How to get URL parameters in Rails?

查看:224
本文介绍了如何在Rails中获取URL参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理POST请求后,params [:id]返回存储在已发布表单中的"id"值.

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

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

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

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

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

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

推荐答案

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

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

def invite

    if request.post? 

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

    end
end

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

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

这篇关于如何在Rails中获取URL参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆