简体   繁体   English

Link_to另一个视图在轨道上的红宝石

[英]Link_to another view ruby on rails

I am trying to use the link_to feature to link one view to another. 我正在尝试使用link_to功能将一个视图链接到另一个视图。

The view i am calling link_to is app/views/instructors/show.html.erb and that snippet of code looks like this (namely, the second to last line of it) 我正在调用link_to的视图是app / views / instructors / show.html.erb,该代码段如下所示(即,倒数第二行)

<% provide(:title, @instructor.login) %>
<% courses = Course.where(:instructor_ID => @instructor.id) %>
    <div class="span2">
      <h1 align=center ><%= @instructor.login %></h1>
      <%= link_to "Add course", new_course_path(:instructor_ID\
                => @instructor.id), :class => "btn" %>
        <br>
        <br>
        <%= link_to "Remove course", delete_course_path(courses), :class => "btn"%>
    </div>

The view I am trying to link to is is app/views/courses/show_all.html.erb and looks like this: 我尝试链接的视图是app / views / courses / show_all.html.erb,看起来像这样:

<% @courses.each do |course| %>
  <tr>
    <td><%= course.course_name %></td>
    <td><%= course.instructor_ID %></td>
    <td><%= link_to 'Show', course %></td>
    <td><%= link_to 'Edit', edit_course_path(course) %></td>
    <td><%= link_to 'Destroy', course, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
  </tr>

delete_course_path routes to app/views/courses/show_all.html.erb shown above. delete_course_path路由到上面显示的app / views / courses / show_all.html.erb。 When I try the code above, I get the following error: 当我尝试上面的代码时,出现以下错误:

undefined method `each' for nil:NilClass nil:NilClass的未定义方法“ each”

At this line: 在这一行:

<% @courses.each do |course| %>

Any ideas what i'm missing in my link_to? 有任何想法我link_to中缺少什么吗?

This means that @courses is nil. 这意味着@courses为零。 Did you set it in your show_all action of your controller? 您是否在控制器的show_all动作中进行了设置? Eg 例如

def show_all
  @courses = Course.all
end

Also, in your show view, you set courses to a collection of Course objects, but your "Remove course" link looks like you only want to delete one course. 另外,在show视图中,您将courses设置为Course对象的集合,但是“删除课程”链接看起来就像您只想删除一个课程。 Why do you use the delete_course route to link to your show_all view? 为什么使用delete_course路线链接到delete_course视图?

In your show_all action, you should define a @courses instance variables. 在你show_all动作,你应该定义一个@courses实例变量。 This is 这是

<% courses = Course.where(:instructor_ID => @instructor.id) %>

not passed to show_all.html.erb . 没有传递给show_all.html.erb

An instance variables is a variable passed from action of controller to the view corresponding. 实例变量是从控制器的动作传递到相应视图的变量。

I suppose when you show page of instructor, your route will like this: /instructors/:id , so maybe in your show_all action of instructor controller, you need something like: 我想当您显示教师页面时,您的路线将像这样: /instructors/:id ,因此也许在您的教师控制器的show_all动作中,您需要这样的东西:

def show_all
  @courses = Course.where(instructor_ID: params[:id])
  render 'courses/show_all'
end

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM