【发布时间】:2016-03-19 23:59:44
【问题描述】:
我在我的 rails 应用程序中收到此错误 undefined method 'comments'。我知道我不应该做 >level 1 嵌套资源,但我不知道在这种情况下如何应用正确的方法。
目前这是我的路线:
resources :performance_indicators do
resources :improvement_actions do
member do
put "like" => "improvement_actions#upvote"
put "unlike" => "improvement_actions#downvote"
end
resources :comments
end
end
正如我所说,我收到了这个错误:
NoMethodError in PerformanceIndicators#show
Showing .../app/views/comments/_form.html.erb where line #1 raised:
undefined method `comments' for nil:NilClass
我不知道我的问题是否出在控制器上。任何人都可以帮忙吗? :)
编辑:
我的评论/_form:
<%= form_for([@performance_indicator, @improvement_action, @improvement_action.comments.build]) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是我的 CommentsController:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :set_improvement_action
# GET /comments
# GET /comments.json
def index
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
@comment = @improvement_action.comments.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
@comment = @improvement_action.comments.new(comment_params)
if @comment.save
format.html { redirect_to [@improvement_action.performance_indicator, @improvement_action], notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: [@improvement_action, @comment] }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
format.json { render :show, status: :ok, location: @comment }
else
format.html { render :edit }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:body)
end
def set_improvement_action
@improvement_action = ImprovementAction.includes(:comments).find(params[:improvement_action_id])
end
end
这是我的 PerformanceIndicatorController:
class PerformanceIndicatorsController < ApplicationController
before_action :set_performance_indicator, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /performance_indicators
# GET /performance_indicators.json
def index
@performance_indicators = PerformanceIndicator.all
end
# GET /performance_indicators/1
# GET /performance_indicators/1.json
def show
#@performance_indicators = PerformanceIndicator.all.order("created_at DESC")
end
# GET /performance_indicators/new
def new
@performance_indicator = PerformanceIndicator.new
@comments = Comment.new
end
# GET /performance_indicators/1/edit
def edit
end
# POST /performance_indicators
# POST /performance_indicators.json
def create
@performance_indicator = PerformanceIndicator.new(performance_indicator_params)
respond_to do |format|
if @performance_indicator.save
format.html { redirect_to @performance_indicator, notice: 'Performance indicator was successfully created.' }
format.json { render :show, status: :created, location: @performance_indicator }
else
format.html { render :new }
format.json { render json: @performance_indicator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /performance_indicators/1
# PATCH/PUT /performance_indicators/1.json
def update
respond_to do |format|
if @performance_indicator.update(performance_indicator_params)
format.html { redirect_to @performance_indicator, notice: 'Performance indicator was successfully updated.' }
format.json { render :show, status: :ok, location: @performance_indicator }
else
format.html { render :edit }
format.json { render json: @performance_indicator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /performance_indicators/1
# DELETE /performance_indicators/1.json
def destroy
@performance_indicator.destroy
respond_to do |format|
format.html { redirect_to performance_indicators_url, notice: 'Performance indicator was successfully destroyed.' }
format.json { head :no_content }
end
end
def set_comment
@improvement_action = ImprovementAction.find(params[:improvement_action_id])
@comment = Comment.find(params[:id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_performance_indicator
@performance_indicator = PerformanceIndicator.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def performance_indicator_params
params.require(:performance_indicator).permit(:name, :numberTimesIdentifiedProblems, :numberTimesAnalysed)
end
end
【问题讨论】:
-
它在您的 cmets 表单中被调用。发布那个。您发布的代码与您的错误无关。你打电话给
something.comments,而something是nil -
我用 cmets 形式编辑了
-
您在哪个视图页面中呈现该部分文件?
-
它在 _improvement_actions 中(这也是对性能指标的部分调用),如下所示: 。结构是这样的:性能指标 -> 改进操作 -> cmets
标签: ruby-on-rails nested comments nested-forms