I'm wondering how params work in 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? But once we get into the create, I'm a little confused. What does the new(params[:question]) get for us? And also in the show, what does finding by params[:id] do? I believe params is sent by the view with information stored inside it? def new @question = Question.new end
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. 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. 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. In this case it pulls out information with an id that matches the id in your params. Where did the params come from? Well the params came from the URL in this case. The URL for a show action looks like this exmaple.com/stuff/1
so in this example the id = 1 and params[:id] = 1
A few points I think you might find helpful are:
new
, show
or some custom action that you may define. new
action when you do Question.new
, it initializes a new instance of question with default values. 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". So with this the @question
now holds an instance of Question
with values supplied by a user and held by the params
hash. 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. If you check the output of rake routes
from a terminal or command prompt, you'll see the following line:
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. This id
and format
are included in the params
hash by Rails and is accessible via params[:id]
and params[:format]
.
So, @question = Question.find(params[:id])
would mean Question.find(1)
for url path /questions/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?
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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.