我在我的应用中有一个与发货模型相关联的评论系统,用户可以拥有多个货件,而货件可以有很多评论。 一切都运作完美唯一的问题是我希望我的整个评论系统显示在我的模型的索引页面中,目前它在显示页面中。
当我尝试将其放在索引页面上时,我收到错误:
undefined method `comments' for nil:NilClass
我的控制器
shipment_controller.rb
class ShipmentsController < ApplicationController
before_action :set_shipment, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /shipments
# GET /shipments.json
def index
@shipments = Shipment.all
end
# GET /shipments/1
# GET /shipments/1.json
def show
@comments = Comment.where(shipment_id: @shipment)
end
# GET /shipments/new
def new
@shipment = Shipment.new
end
# GET /shipments/1/edit
def edit
end
# POST /shipments
# POST /shipments.json
def create
@shipment = current_user.shipments.new(shipment_params)
respond_to do |format|
if @shipment.save
format.html { redirect_to @shipment, notice: 'Shipment was successfully created.' }
format.json { render :show, status: :created, location: @shipment }
else
format.html { render :new }
format.json { render json: @shipment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /shipments/1
# PATCH/PUT /shipments/1.json
def update
respond_to do |format|
if @shipment.update(shipment_params)
format.html { redirect_to @shipment, notice: 'Shipment was successfully updated.' }
format.json { render :show, status: :ok, location: @shipment }
else
format.html { render :edit }
format.json { render json: @shipment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /shipments/1
# DELETE /shipments/1.json
def destroy
@shipment.destroy
respond_to do |format|
format.html { redirect_to shipments_url, notice: 'Shipment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_shipment
@shipment = Shipment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def shipment_params
params.require(:shipment).permit(:name, :description, :from, :to, :date, :pay)
end
end
comments_controller.rb
class CommentsController < ApplicationController
before_action :authenticate_user!
def create
@shipment = Shipment.find(params[:shipment_id])
@comment = Comment.create(params[:comment].permit(:content))
@comment.user_id = current_user.id
@comment.shipment_id = @shipment.id
if @comment.save
redirect_to shipment_path(@shipment)
else
render 'new'
end
end
end
我的路线
Rails.application.routes.draw do
devise_for :users, :controllers => {:registrations => "registrations"}
resources :shipments do
resources :comments
end
root 'shipments#index'
end
货件视图
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong><%= current_user.full_name %></strong>
</p>
<p>
<strong>Description:</strong>
<%= @shipment.description %>
</p>
<p>
<strong>From:</strong>
<%= @shipment.from %>
</p>
<p>
<strong>To:</strong>
<%= @shipment.to %>
</p>
<p>
<strong>Date:</strong>
<%= @shipment.date %>
</p>
<p>
<strong>Pay:</strong>
<%= @shipment.pay %>
</p>
<div id="comments">
<h2 class="comment_count">
<%= pluralize(@shipment.comments.count, "comment") %>
</h2>
<% @comments.each do |comment| %>
<div class="comment">
<p class="full_name">
<%= comment.user.full_name %>
</p>
<p class="content">
<%= comment.content %>
</p>
</div>
<% end %>
<%= render "comments/form" %>
</div>
<%= link_to 'Edit', edit_shipment_path(@shipment) %> |
<%= link_to 'Back', shipments_path %>
评论视图渲染
_form.html.erb
<%= simple_form_for([@shipment, @shipment.comments.build]) do |f| %>
<%= f.input :content, label: "Reply to thread" %>
<%= f.button :submit, class: "button" %>
<% end %>
index.html.erb没有评论
<h1>Listing Shipments</h1>
<%= link_to "Post a new Shipment", new_shipment_path, class: "btn btn-success" %>
<% @shipments.each do |shipment| %>
<div class="shipment">
<h3><strong><%= shipment.user.full_name%></strong></h3>
<h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
<div class="meta">
<%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |
<%= link_to "show", shipment %>
<span class="admin">
| <%= link_to "Edit", edit_shipment_path(shipment) %> |
<%= link_to "Delete", shipment, method: :delete, data: { confirm: "Hey! Are you sure! You wanna delete this shipment??"} %>
</span>
</div>
</div>
<% end %>
带注释系统的index.html.erb
<h1>Listing Shipments</h1>
<%= link_to "Post a new Shipment", new_shipment_path, class: "btn btn-success" %>
<% @shipments.each do |shipment| %>
<div class="shipment">
<h3><strong><%= shipment.user.full_name%></strong></h3>
<h5><strong>DESCRIPTION: </strong><%= shipment.description %></h5>
<div class="meta">
<%= link_to time_ago_in_words(shipment.created_at) + " ago" %> |
<%= link_to "show", shipment %>
<span class="admin">
| <%= link_to "Edit", edit_shipment_path(shipment) %> |
<%= link_to "Delete", shipment, method: :delete, data: { confirm: "Hey! Are you sure! You wanna delete this shipment??"} %>
</span>
</div>
</div>
<div id="comments">
<h2 class="comment_count">
<%= pluralize(@shipment.comments.count, "comment") %>
</h2>
<% @comments.each do |comment| %>
<div class="comment">
<p class="full_name">
<%= comment.user.full_name %>
</p>
<p class="content">
<%= comment.content %>
</p>
</div>
<% end %>
<%= render "comments/form" %>
<% end %>
Pavan的回答后
错误的
ActiveRecord::RecordNotFound (Couldn't find Shipment with 'id'=):
app/controllers/shipments_controller.rb:9:in `index'
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
(1.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within re
scues/layout (36.0ms)
Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/_markup.html.erb (1.0ms)
答案 0 :(得分:0)
你有一个似乎没有定义的ivar @comments
...尝试@shipment.comments.each
答案 1 :(得分:0)
我希望我的整个评论系统显示在我的索引页面中 模型目前在显示页面
您需要在 索引视图 中循环浏览@shipments
,如下所示
<% @shipments.each do |shipment| %>
<% shipment.comments.each do |comment| %>
----rest of the code---
<% end %>
<% end %>
<强> 更新 强>
您在此行<%= pluralize(@shipment.comments.count, "comment") %>
中收到错误。您只需将其更改为
<%= pluralize(shipment.comments.count, "comment") %>
你需要像我上面所说的那样修改评论迭代部分。