简体   繁体   中英

What are Ruby on Rails Controller Parameters?

What are the parameters sent by rails when they are sending them via hash as seen in the terminal while the server is running? This is the section in the terminal I'm talking about.

Started POST "/topics/2/feeds" for 127.0.0.1 at 2014-04-01 21:01:17 -0700
Processing by FeedsController#create as JS
# HERE...
Parameters: {"feed"=>{"text"=>"Hello!"}, "commit"=>"Create Feed", "topic_id"=>"2"}

In the RailsGuides they just say it is a query string in the url or POST data, but is it just Ruby code? Can it be JSON?

Parameters are a simple hash that is received from the client, either via the query string or as POST data, just as the doc states. To access it in a controller, Rails makes the params hash available.

For your example above, you can simply use params[:id] to get the value 2 . Rails will have some keys and values by default if you use the Rails forms. Others you can specify on the client, usually using some sort of ajax query.

It's not really a hash. What you see in the console, Parameters: {"id"=>"2"} , is simply the representation of the parameters for human readability. In the particular case you present, the parameter is given in the url:

GET /topics/2

If you look into your config/routes.rb , you will probably find a line like:

resources :topics

That means that the TopicsController will be treated as a resource, and when you make this request,

GET /topics/2

Rails will interpret it that you are asking for a topic resource with id 2 . So the request will be pointed to the method show of your controller, and your params hash will have an id key with value 2 .

Read about Rails Routing .

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.

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