When displaying the article details, I wanted to display the user name, but I did not know where it was stored, so I decided to examine the contents of params. Keep a log of the process.
Install gem (gem'pry-rails')
↓
bundle install
↓
Enter binding.pry in the relevant part you want to check.
↓
rails s
↓
Click on the relevant part with your browser. The terminal shows the log that stopped at the point you entered.
↓
And [1] pry (#
ruby:qiita.controller.rb
12: def show
13: #I want to get only one post.
14: @post = Post.find(params[:id])
=> 15: binding.pry
16: end
Try from top to bottom id params[:id] @post.content @post.user I tried to enter!
[1] pry(#<PostsController>)> id
NameError: undefined local variable or method `id' for #<PostsController:0x00007f9f26ea4828>
from (pry):1:in `show'
[2] pry(#<PostsController>)> params[:id]
=> "1"
[3] pry(#<PostsController>)> @post.content
=> "Temporibus vel ratione aperiam alias aut libero reiciendis voluptatem quo autem rerum doloribus adipisci a voluptas modi illo qui ipsum aliquid voluptatum nventore at esse maiores ut omnis accusantium animi ducimus qui autem architecto excepturi itaque ex minus facere soluta inventore molestias id unde vero sunt aliquam quia dolorum quae placeat deserunt aspernatur qui suscipit quod dolorem maxime nulla id molestiae incidunt aut beatae aut voluptate aliquid dicta velit sit sint eum possimus nihil non voluptatem provident enim assumenda consequatur fugiat."
[4] pry(#<PostsController>)> @post.user
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ (pry):4:in `show'
=> #<User:0x00007f9f26c77b68
id: 1,
name: "swifty_kazu",
email: "hogehogehoge",
created_at: Fri, 16 Oct 2020 02:53:43 UTC +00:00,
updated_at: Fri, 16 Oct 2020 02:53:43 UTC +00:00,
password_digest: [FILTERED],
admin: true>
[5] pry(#<PostsController>)> @post.user.name
=> "swifty_kazu"
[6] pry(#<PostsController>)>
You can exit with exit!
Reference article https://qiita.com/tomoharutt/items/6b12af3dc5eb8dfb9801 https://pikawaka.com/rails/params#params%E3%81%AE%E4%B8%AD%E8%BA%AB%E3%82%92%E7%A2%BA%E8%AA%8D%E3%81%97%E3%81%A6%E3%81%BF%E3%82%88%E3%81%86 https://qiita.com/k0kubun/items/b118e9ccaef8707c4d9f
Recommended Posts