RM3
RM3

Reputation: 107

Where do the keys in the params array come from?

This question has been asked before, but this is a more specific instance.

In the Ruby on Rails "Getting Started" [ http://guides.rubyonrails.org/getting_started.html ] web page, they're teaching you how to make a simple blog. I'm following most of it, but I do not get where this :article_id key value is coming from:

def create
    @article = Article.find(params[:article_id])
    ...some other stuff...
end

This is located in the commentsController of the web app. All article related coding is in the articlesController.

Is the underscore an indicator to the class the id belongs to? Meaning does this key value first find the article params array and then the id all by itself? Like as a feature of RoR? Or is the symbol just being put there for teaching purposes and does not ACTUALLY refer to anything?

If it is the latter case, how would you know the key value to use?

GET data is shown in the URL, but how would you set that to know what it is before ever calling up the URL?

Upvotes: 1

Views: 101

Answers (1)

user229044
user229044

Reputation: 239240

Your routes.rb defines routes, the matched route may have variable segments in it, and the variable segments are made available to you via the params hash in your controller.

If you're using params[:article_id], the route that lead you to that action would have contained something like:

 /articles/:article_id

params will also contain any values passed along via the query string or form data, but as far as looking up records by their ID, the ID is usually a component of the URL.

Is the underscore an indicator to the class the id belongs to?

No, it's simply part of the name.

If it is the latter case, how would you know the key value to use?

Because you define the key to use in your routes file.

Upvotes: 4

Related Questions