简体   繁体   English

如何在特定表格中显示特定属性,而不为当前用户显示所有属性RAILS 4

[英]How can i display a certain attribute in a certain table without displaying all for a current user RAILS 4

I have a student and school database tied to my rails project I have these attributes for student table : 我有一个与我的Rails项目绑定的studentschool数据库,我具有这些Student表的属性:

[id,fname,lname,created_at,updated_at,school_id,user_id]

and my school table has these attributes 我的学校桌子有这些属性

[id,name,address,franchise_id,created_at,updated_at]

so far I am trying to display the school name for each student that a current user may have. 到目前为止,我正在尝试显示当前用户可能拥有的每个学生的学校name but when I run it, it will show all the schools for all the students of that user on each iteration of the loop. 但是当我运行它时,它将在循环的每次迭代中显示该用户所有学生的所有学校。 here is my index action method where the magic is supposed to happen. 这是应该发生魔术的索引操作方法。

  @child = Student.where(user_id:current_user.id).pluck(:school_id)
  @schoolname = School.where(id:@child).pluck(:name)

and my index page in my view: 和我的索引页在我看来:

 <div class="container">
 <h1><font color="white"><b>My students</font></b></h1>

 <table class="table table-striped">
 <thead>
  <tr>
  <th><font color="white"><b>Id</font></b></th>
  <th><font color="white"><b>Fname</font></b></th>
  <th><font color="white"><b>Lname</font></b></th>
  <th><font color="white"><b>School</font></b></th>
  <th><font color="white"><b>manage</font></b></th>
  <th colspan="3"></th>
   </tr>
  </thead>

  <tbody>
   <% @students.each do |student| %>
      <tr>
        <td><font color="white"><b><%= student.id %></font></b></td>
       <td><font color="white"><b><%= student.fname %></font></b></td>
       <td><font color="white"><b><%= student.lname %></font></b></td>
       <td><font color="white"><b><%= @schoolname %></font></b></td>
       <td><%= link_to 'Show', student %>
       <%= link_to 'Edit', edit_student_path(student) %>
       <%= link_to 'Destroy', student, method: :delete, data: { confirm: 
       'Are you sure?' } %></td>
       </tr>
        <% end %>
        </tbody>
       </table>

       <br>

       <b><%= link_to 'Add Student', add_students_path(@student),
       {:style=>'color:#FFFFFF;'} %></b><br/>

       <b> <%= link_to 'New Student', new_student_path, 
        {:style=>'color:#FFFFFF;'} %></b>
          </div>

my model is empty 我的模特是空的

而不是在视图循环中使用@schoolname,而是编写student.school.name如果您的一对多关系有效,那么应该可以。

Sorry I don't the ability to comment but I wanted to ask where you assign @students ? 抱歉,我没有发表评论的能力,但我想问一下@students在哪里分配? You use it in your view but I don't see it in your controller's index method. 您在视图中使用它,但在控制器的index方法中看不到它。 Also @student should be student in the link_to methods at the end. @student student也应该是link_to方法中的student

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取和显示sql表中某个字段的所有值? - How can I get and display all values of a certain field in a sql table? 如何将某个用户限制为MySQL数据库中的一个表 - How can I restrict a certain user to one table in a MySQL database 如何检查当前用户是否对 MySQL/MariaDB 有一定的权限? - How can I check if current user has certain privilege on MySQL/MariaDB? 显示表格结果而不重复某些值 - Display table results without repeating certain values Laravel 5:如何检索和显示属于某个类别的所有帖子 - Laravel 5: how can I retrieve and display all posts that belong to certain category 如何计算某个用户的按钮点击次数,然后显示计数? - How do I count button clicks for a certain user and then display the count? 如何不显示数据库中表的某些部分? - How do I not display certain parts of the table from my database? 如何在不真正加入该表的情况下通过连接获取某些字段值? - How can I take certain field value with a join without really joining that table? 如何在不使用ENUM的情况下在MySQL表中强制执行某些字符串? - How can I enforce certain strings in a MySQL table without using ENUMs? 如何在 Codeigniter 中显示具有特定字段绑定条件的字段? - How can I display a field with a certain field bound condition in Codeigniter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM