简体   繁体   English

Rails:参数如何在控制器中工作?

[英]Rails: how do params work in controller?

I'm wondering how params work in rails. 我想知道参数如何在Rails中工作。 So I have a questions controller. 所以我有一个问题控制器。 The first action new means that I call upon the create new function for a model Question right? 新的第一个动作意味着我需要为模型调用create new函数问对吗? But once we get into the create, I'm a little confused. 但是一旦进入创建阶段,我就会有些困惑。 What does the new(params[:question]) get for us? new(params [:question])为我们带来了什么? And also in the show, what does finding by params[:id] do? 而且在节目中,通过params [:id]查找有什么作用? I believe params is sent by the view with information stored inside it? 我相信参数是由视图中存储的信息发送的吗? def new @question = Question.new end def new @question = Question.new结束

def create
    @question = Question.new(params[:question])
    if @question.save
        redirect_to @question
    else
        render 'new'
    end
end

def show
    @question = Question.find(params[:id])
end

What does the new(params[:question]) get for us?

The create action is hit when you press something like a submit button. 当您按下诸如“ submit按钮之类的命令时,将创建一个创建动作。 Then that form is sent via params to your controller. 然后,该表格通过参数发送到您的控制器。 Your controller then makes a new object. 然后,您的控制器将创建一个新对象。 Basically new is just making a container where you can put stuff. 基本上,新的东西只是制造一个可以放东西的容器。 That stuff is the params. 那东西就是参数。

in the show, what does finding by params[:id] do?

in the show you are calling .find on a model. 在节目中,您正在模型上调用.find。 Models are the capitalized words in your controller. 模型是控制器中的大写单词。 Models interact with your database. 模型与您的数据库进行交互。 When you call find (which is an active record method) the model goes into your database and pulls out information. 当您调用find(这是一个有效的记录方法)时,模型将进入数据库并提取信息。 In this case it pulls out information with an id that matches the id in your params. 在这种情况下,它会提取ID与您的参数中的ID匹配的信息。 Where did the params come from? 这些参数来自哪里? Well the params came from the URL in this case. 在这种情况下,参数来自URL。 The URL for a show action looks like this exmaple.com/stuff/1 so in this example the id = 1 and params[:id] = 1 show动作的URL类似于exmaple.com/stuff/1因此在此示例中,id = 1,params [:id] = 1

A few points I think you might find helpful are: 我认为您可能会发现有帮助的几点是:

  1. The order of actions defined in a controller is not important. 控制器中定义的动作顺序并不重要。 So, the first action can be anything, it can be new , show or some custom action that you may define. 因此,第一个动作可以是任何东西,可以是newshow或您可以定义的一些自定义动作。
  2. In the new action when you do Question.new , it initializes a new instance of question with default values. 在执行Question.newnew操作中,它将使用默认值初始化一个新的question实例。
  3. In the create action when you do Question.new(params[:question]) , it initializes a new Question instance assigning attribute values from the params hash that are allowed through "mass assignment". 在执行Question.new(params[:question])create动作中,它将初始化一个新的Question实例,该实例从params哈希中分配属性值,这些值通过“质量分配”来允许。 So with this the @question now holds an instance of Question with values supplied by a user and held by the params hash. 因此, @question现在拥有一个Question实例,该实例的值由用户提供,并由params哈希值保存。

In the show action which is executed when visiting a url such as /questions/1 , the 1 usually is id unless you've specifically specified a different attribute in your config/routes.rb configuration. 在访问诸如/questions/1类的URL时执行的show action中,除非您在config/routes.rb配置中特别指定了其他属性,否则1通常是id If you check the output of rake routes from a terminal or command prompt, you'll see the following line: 如果您从终端或命令提示符处检查rake routes的输出,则会看到以下行:

question GET    /questions/:id(.:format)          questions#show

What this line means is that for any URL pattern /questions/:id(.:format) where :id is the resource id, in this case question id with an optional parameter format, execute the show action of questions controller. 这行的意思是,对于任何URL模式/questions/:id(.:format) ,其中:id是资源ID,在这种情况下,问题id具有可选的参数格式,请执行questions控制器的show操作。 This id and format are included in the params hash by Rails and is accessible via params[:id] and params[:format] . 这个idformat由Rails包含在params哈希中,可以通过params[:id]params[:format]

So, @question = Question.find(params[:id]) would mean Question.find(1) for url path /questions/1 . 因此, @question = Question.find(params[:id])表示URL路径/questions/1 Question.find(1)

I find that the best way to think of this is to hit up the good old command line. 我发现,考虑到这一点的最佳方法是使用良好的旧命令行。

irb(main):001:0> Thing.new
=> #<Thing id: nil, name: nil, description: nil, url: nil, image: nil, created_at: nil, updated_at: nil>

Now.. what happens when you pass an argument of an attribute to Thing.new? 现在,当您将属性的参数传递给Thing.new时会发生什么?

irb(main):002:0> Thing.new(:name => 'Foo')
=> #<Thing id: nil, name: "Foo", description: nil, url: nil, image: nil, created_at: nil, updated_at: nil>

So what's in params? 那么,什么是参数呢? Well, in params[:question] you are passing a hash of attributes. 好吧,在params [:question]中,您正在传递属性的哈希。 So really, you are doing what I just did above. 所以说真的,您正在做我上面所做的事情。 Now... on Thing here... I have validations that would prevent a Thing from saving with just a name... so if I tried to save... I get false. 现在...在这里的东西上...我已经进行了验证,可以防止仅用名称保存东西...所以如果我尝试保存...我会得到错误的结果。

irb(main):004:0> thing = Thing.new(:name => 'Foo')
=> #<Thing id: nil, name: "Foo", description: nil, url: nil, image: nil, created_at: nil, updated_at: nil>
irb(main):005:0> thing.save
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK
=> false

That's why there is the if @question.save branch 这就是为什么有if @ question.save分支的原因

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

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