Ich bekomme diesen Fehler undefined method 'comments'
in meiner Rails-Anwendung. Ich weiß, dass ich keine verschachtelten Ressourcen der Stufe 1 machen sollte, aber ich weiß nicht, wie ich in diesem Fall den richtigen Weg finde.Rails: tief verschachtelte Ressourcen gibt Fehler: undefinierte Methode 'Kommentare'
Derzeit ist dies meine Routen:
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
Wie gesagt ich bekomme diese Fehlermeldung:
NoMethodError in PerformanceIndicators#show
Showing .../app/views/comments/_form.html.erb where line #1 raised:
undefined method `comments' for nil:NilClass
Ich weiß nicht, ob mein Problem in der Steuerung ist. Jeder kann helfen? :)
EDIT:
Mein Beitrag/_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 %>
Das ist mein CommentsController ist:
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
ist hier mein 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
Es wird in Ihrem Kommentarformular aufgerufen. Posten Sie das. Der von Ihnen gepostete Code ist für Ihren Fehler nicht relevant. Sie rufen 'something.comments' und das' something' ist 'nil' – toddmetheny
ich bearbeitet, mit dem Kommentarformular –
In welcher Ansichtsseite rendern Sie diese Teildatei? – Pavan