【问题标题】:How to loop my node.name on list/index.html.erb如何在 list/index.html.erb 上循环我的 node.name
【发布时间】:2016-03-29 17:37:41
【问题描述】:

在一个教程中,我知道一些这样的 link_to 代码。

lists/index.html.erb

<ul class="lists box">
<% lists.each do |list| %>
    <li>
        <h2 class="list_title">
            <%= link_to list.title, list %>
        </h2>
        <p>
          <%= truncate(list.content, length: 99) %>
          <br>
          <%= link_to list.node.name, list.node, class: "node" %>
        </p>
    </li>
<% end %>
</ul>

还有两张桌子

  create_table "lists", force: :cascade do |t|
    t.string   "title"
    t.text     "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.integer  "node_id"
  end

  create_table "nodes", force: :cascade do |t|
    t.string   "name"
    t.string   "summary"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

2 个模型

class Node < ActiveRecord::Base
    has_many :lists
end

class List < ActiveRecord::Base
    belongs_to :node 
end

控制器也可以

class ListsController < ApplicationController
  before_action :set_list, only: [:show, :edit, :update, :destroy]

  def index
    @lists = List.all.order(created_at: :desc).page(params[:page]).per(15)
  end

  def show
    @list = List.find(params[:id])
  end
.
.
.
  private
    def set_list
      @list = List.find(params[:id])
    end

    def list_params
      params.require(:list).permit(:title, :content)
    end
end

另一个控制器

class NodesController < ApplicationController
  before_action :set_node, only: [:show, :edit, :update, :destroy, :node_list]


  def index
    @nodes = Node.all
  end

  def show
    @node = Node.find(params[:id])
  end
.
.
.
.

我的路线

Rails.application.routes.draw do
  resources :lists, only: [:index, :show, :new]
  devise_for :users
  resources :users
  resources :nodes, only: [:show] 

  root 'lists#index'
end

当我 rails s 时,我得到了错误

NoMethodError in Lists#index
Showing /Users/zhangxiaodong/workspace/listlist/app/views/lists/_list_list.html.erb where line #10 raised:

undefined method `name' for nil:NilClass

当我改变 link_to &lt;%= link_to list.node.name, list.node, class: "node" %&gt;&lt;%= link_to "list.node.name", list.node, class: "node" %&gt; 页面没有错误,但 node.name 链接循环不在页面上。 我看 API link_to [http://api.rubyonrails.org/],没用,你能告诉我如何循环我的节点。

【问题讨论】:

  • 你能显示你的routes.rb文件内容吗?
  • 谢谢,我编辑我的问题。

标签: ruby-on-rails tags link-to


【解决方案1】:

lists/index.html.erb 应如下所示:

<ul class="lists box">
  <% lists.each do |list| %>
    <li>
      <h2 class="list_title">
        <%= link_to list.title, list_path(list) %>
      </h2>
      <p>
        <%= truncate(list.content, length: 99) %>
      <br>
        <%= link_to list.node.name, node_path(list.node), class: "node" if list.node.present? %>
     </p>
   </li>
 <% end %>
</ul>

【讨论】:

    【解决方案2】:

    因为您的list 可能没有任何node。像这样修改代码就好了

    旧代码

    <%= link_to list.node.name, list.node, class: "node" %>
    

    新代码

    <% if list.node %>
      <%= link_to list.node.name, list.node, class: "node" %>
    <% end %>
    

    【讨论】:

      【解决方案3】:

      尝试以下方法:

      lists/index.html.erb

      <ul class="lists box">
      <% @lists.each do |list| %>
          <li>
              <h2 class="list_title">
                  <%= link_to list.title, list_path(list) %>
              </h2>
              <p>
                <%= truncate(list.content, length: 99) %>
                <br>
                <%= link_to list.node.name, node_path(list.node), class: "node" %>
              </p>
          </li>
      <% end %>
      </ul>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 2012-01-23
        • 2011-02-19
        • 2011-04-01
        • 1970-01-01
        • 2022-11-11
        • 1970-01-01
        相关资源
        最近更新 更多