Home>

I am making a system in Rails where users post products (nail).
I want to display the content posted by only current_user on my page of user when user posts the product.
It is not reflected at all.
(All information is reflected on the index screen of nail, and it can be displayed without any problem)

My page I want to display
   My Page       <% @ nails.each do | nail |%>     <% = nail.name%>     <% = attachment_image_tag nail ,: image,: fill, 50, 50, format: "jpeg"%>     <% = nail.price%>     <% = nail.introduction%>    <% end%><% = link_to "new post", new_nail_path%>________
user controller
class Users :: UserController</pre>
<strong>routing</strong>
<pre><code data-language = "route">Rails.application.routes.draw do
  devise_for: users, controllers: {
    registrations:'users/registrations',
    sessions: "users/sessions",
  }
namespace: users do
    resources: user, only: [: show,: edit]
    resources: relationships
  end
  resources: nails do
      resource: favorites, only: [: create,: destroy]
      resources: nail_comments, only: [: index ,: show ,: edit ,: new ,: destroy ,: create]
  end
  root'home # top'# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
New post screen for nails
<% = form_with model: @nail, url: nails_path, method:: post, local: true do | f |%> <% = f.hidden_field: nail_id%>  <table>    <tr>        <th><% = f.label: user,'name'%></th>        <th><% = f.label: name,'Product name'%></th>        <th><% = f.label: price,'price'%></th>        <th><% = f.label: image,'Product image'%></th>        <th><% = f.label: introduction,'Product description'%></th>    </tr>    <tr>        <td><% = f.text_field: user%></td>        <td><% = f.text_field: name%></td>        <td><% = f.text_field: price%>Yen</td>        <td><% = f.attachment_field: image%></td>        <td><% = f.text_area: introduction%></td>    </tr>    <tr>        <td><% = f.submit'submit'%></td>    </tr>  </table><% end%>
Nail controller
class NailsController</pre>
<pre><code data-language = "model">class Nail</pre>
<strong>What I tried</strong>
<p>I changed the description method of the user controller several times, but the result did not change.</p>
<pre><code>@user = current_user
@nails = Nail.where (user_id: @ user.id)
→ @nails = @ user.nails.all


Please describe here what you have tried for the problem.

I looked it up in binding.pry
  3: def show
    4: @user = current_user
    5: @nails = Nail.where (user_id: @ user.id)
    6: binding.pry
 =>7: end
[1] pry (#<Users :: UserController>)>@nails
  Nail Load (2.1ms) SELECT "nails". * FROM "nails" WHERE "nails". "User_id" =? [["User_id", 3]]
  ↳ app/controllers/users/user_controller.rb: 7
=>[#<Nail: 0x00000000054be060
  id: 17, id: 17,
  created_at: Sat, 14 Nov 2020 03:43:42 UTC +00: 00,
  updated_at: Sat, 14 Nov 2020 03:43:42 UTC +00: 00,
  relationship_id: nil,
  user_id: 3,
  name: nil,
  image_id: nil,
  brand: nil,
  introduction: nil,
  price: nil>]


By the way, on the show screen
If i try @nails = Nail.all, all the posted contents can be reflected.

Supplementary information (FW/tool version, etc.)

Rails 5.2.4.3

This is a beginner and first question, so please point out any shortages.
I look forward to working with you.

  • Answer # 1

    Will My Page itself be displayed? (h2 My Page and New Post Button)
    @ nails.each do | nail |Just beforebinding.pryInsert@nailsWhat happens if I check the contents?

    <% binding.pry%>
    <% @ nails.each do | nail |%>