[RUBY] The identity of params [: id] in rails

The data you receive when defining actions in rails

params[:id]

I often expressed it as, but I didn't understand the meaning, so I made a note. I hope it will be helpful for those who are suffering from similar problems.

Prerequisites

For example, suppose you want to implement a function to check the details of a post on the screen that displays the post like this. <img width = "798" alt = "Myblog.png " src = "https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/652172/60c1bb16-5d35-51f3-9bdf" -ec24eb3273ee.png "> Since the action to be displayed in detail is a show action, it seems good to define a show action in the controller and prepare a corresponding view.

Show action settings

I think that show actions are often expressed as follows on many rails learning sites.

  def show
    @post = Post.find(params[:id])
  end

This can be decomposed as follows, and each has the following meanings. Post… Post model. Post data stored in the database find… How to get by specifying the one that matches the condition from the Post model params [: id]:… Conditions for data fetched by find params… Data that sticks when you open the URL of a specific tweet : id… id associated with the tweet

In other words, this means that the data corresponding to the id of the post that the user is trying to access will be picked up from the Post model.

@post… Assign the above to an instance variable so that it can be used with external data (or view)

So, by assigning it to an instance variable, it can be displayed in the view file.

When I actually check the routing

post GET    /posts/:id(.:format)              posts#show

The routing path corresponding to the show action of the posts controller is post /: id. Since this: id corresponds to the post here, I want to display the post with the same id in the view called by the show action. So the show action defines @post and loads it into the view as described above.

Recommended Posts

The identity of params [: id] in rails
[Order method] Set the order of data in Rails
The problem that the contents of params are completely displayed in the [Rails] view
SSL in the local environment of Docker / Rails / puma
(Ruby on Rails6) Display of the database that got the id of the database
[Rails] Where to be careful in the description of validation
[Rails] Check the contents of the object
About the symbol <%%> in Rails erb
Explanation of the order of rails routes
Check the migration status of rails
Get the ID of automatic numbering
Order of processing in the program
How to get the id of PRIMAY KEY auto_incremented in MyBatis
Replace preview by uploading by clicking the image in file_field of Rails
Get the result of POST in Java
Check the contents of params with pry
part of the syntax of ruby ​​on rails
[rails] List of actions defined in Controller
Rails refactoring story learned in the field
The story of AppClip support in Anyca
[Rails] Change the label name of f.label
The story of writing Java in Emacs
The process of introducing Vuetify to Rails
[Rails] Reset the database in the production environment
Rails6: Extract the image in Action Text
Write the movement of Rakefile in the runbook
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
[Rails] Regarding the presence or absence of parentheses in the argument of the render method
[Rails] The cause of not being able to post was in form_with
[Ruby On Rails] How to search the contents of params using include?
Get the value of enum saved in DB by Rails with attribute_before_type_cast
Change the save destination of the image to S3 in the Rails app. Part 2
Decompose "$ printf" install: --no-document \ nupdate: --no-document \ n ">> ~ / .gemrc" in Chapter 1 of the Rails Tutorial
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
[Rails] Button to return to the top of the page
The story of making ordinary Othello in Java
[Ruby on Rails] Until the introduction of RSpec
About the idea of anonymous classes in Java
I tried to organize the session in Rails
How to get the ID of a user authenticated with Firebase in Swift
The story of learning Java in the first programming
Measure the size of a folder in Java
Recommendation of Service class in Ruby on Rails
[Rails] Show multi-level categories in the breadcrumb trail
Feel the passage of time even in Java
[Rails] Implementation of retweet function in SNS application
Group_by in Rails
[Rails] Implementation of "notify notification in some way"
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
How to use JQuery in js.erb of Rails6
Implement the Like feature in Ajax with Rails.
How to make a unique combination of data in the rails intermediate table
Validate the identity token of a user authenticated with AWS Cognito in Java
Import files of the same hierarchy in Java
How to check Rails commands in the terminal
[Rails] Ranking and pagination in order of likes
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
Install multiple submit buttons in Rails View to get the value of the pressed button
[Rails] I want to display the link destination of link_to in a separate tab