Rails 参数解释? [英] Rails params explained?

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

问题描述

谁能解释 Rails 控制器中的 params:它们来自哪里,它们引用了什么?

Could anyone explain params in Rails controller: where they come from, and what they are referencing?

  def create
    @vote = Vote.new(params[:vote])
    item = params[:vote][:item_id]
    uid = params[:vote][:user_id]
    @extant = Vote.find(:last, :conditions => ["item_id = ? AND user_id = ?", item, uid])
    last_vote_time = @extant.created_at unless @extant.blank?
    curr_time = Time.now
  end

我希望能够逐行阅读此代码并了解发生了什么.

I would like to be able to read this code line-by-line and understand what's going on.

推荐答案

当用户请求页面时,参数来自用户的浏览器.对于最常见的 HTTP GET 请求,参数在 url 中编码.例如,如果用户的浏览器请求

The params come from the user's browser when they request the page. For an HTTP GET request, which is the most common, the params are encoded in the url. For example, if a user's browser requested

http://www.example.com/?foo=1&boo=章鱼

然后 params[:foo] 将是1",而 params[:boo] 将是章鱼".

then params[:foo] would be "1" and params[:boo] would be "octopus".

在 HTTP/HTML 中,参数实际上只是一系列键值对,其中键和值都是字符串,但是 Ruby on Rails 有一种特殊的语法可以使参数成为内部带有散列的散列.例如,如果用户的浏览器请求

In HTTP/HTML, the params are really just a series of key-value pairs where the key and the value are strings, but Ruby on Rails has a special syntax for making the params be a hash with hashes inside. For example, if the user's browser requested

http://www.example.com/?投票[item_id]=1&vote[user_id]=2

然后 params[:vote] 将是一个哈希,params[:vote][:item_id] 将是1"和 params[:vote][:user_id] 将是2".

then params[:vote] would be a hash, params[:vote][:item_id] would be "1" and params[:vote][:user_id] would be "2".

Ruby on Rails 参数相当于 PHP 中的 $_REQUEST 数组.

The Ruby on Rails params are the equivalent of the $_REQUEST array in PHP.

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

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