Home>
Prerequisite/I want to realize

I'm making an "instruction/contact application for internal use".

What i am trying to do

When an instruction is sent from the store manager (user) to the employee (staff)
Enter the name of the employee who will give instructions in the drop-down
Enter and submit the instructions (comment)
The store manager has a list of instructions sent by him
Employees want to be able to see the comments they have received.

Problem i am experiencing

I tried to see a list of instructions that came to me.
Use an if statement because it's for you The place where the comment is displayed when the staff and name match (micropost.staff == user.name)
I got an error and couldn't proceed.
] (d31e3e3bf2713761c3affb4d434023e0.png) p> Source code

migrate/microposts

class CreateMicroposts</pre>
<p>migrate/users</p>
<pre><code data-language = "rails">class CreateUsers</pre>
<p>views/microposts/index</p>
<pre><code data-language = "rails">Instructions for yourself 
<ul>
  <% @ microposts.each do | micropost |%>
    <% if micropost.staff == user.name%>
      <li><% = micropost.comment%></li>
    <% end%>
  <% end%>
</ul>

microposts_controller

def index
    @microposts = Micropost.all
    @user = User.find (params [: id])
  end


Is the data in the controller not getting well? Kana
I visited the site and found something called the joins method.
where @user = User.find (params [: id])
I changed it to @microposts = Micropost.joins (: user) but it also gives an error
I'm feeling clogged, so I'll ask a question.

  • Answer # 1

    If the employee has a login function, and the logged-in employee can be taken by current_user

    def index
      @microposts = Micropost.where (staff: current_user.name)
    end

    Aside story
    I can't distinguish employees with the same name, but will they cover the operation?
    (Tanaka A Tanaka B Tanaka C ... If B retires, is it a permanent missing number?)

Related articles