Hallo da habe ich einen Fehler wie diese undefined local variable or method 'post' for #<#<Class:0x007ffc40469ad0>:0x007ffc40461088>
hinzufügen Kommentare zu Beiträgen = undefiniert lokale Variable oder Methode `Post‘ für
Ich verstehe nicht, weil ich eine teilweise in Posten, den Namen _comments.html.erb
<p class="text-center">Poster un commentaire</p>
<%= simple_form_for [post, post.comments.new] do |f| %>
<%= f.error_notification %>
<%= f.input :content, label: "Commentaire"%>
<%= f.submit "Envoyer", class: "btn btn-primary" %>
<% end %>
erstellen
und macht es die wie <% render 'comments' %>
die undefinierte Methode in dieser Linie ist <%= simple_form_for [post, post.comments.new] do |f| %>
das Modell Post has_many :comments, dependent: :destroy
das Modell Post ist has_many :comments, dependent: :destroy
das Modell Kommentar ist belongs_to :user belongs_to :post
Die Route Ressourcen: Beiträge tun Ressourcen: Kategorien Ressourcen: Kommentare Ende
Kommentare Controller ist
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
@comment.user_id = current_user.id
if @comment.save
flash[:success] = "You commented the hell out of that post!"
redirect_to :back
else
flash[:alert] = "There is a problem with your comment"
render root_path
end
end
def destroy
@comment = @post.comments.find(params[:id])
@comment.destroy
flash[:success] = "Comment deleted :("
redirect_to root_path
end
private
def set_post
@post = Post.find(params[:post_id])
end
def comment_params
params.require(:comment).permit(:content, :post_id, :user_id)
end
end
Vielen Dank für Ihre Hilfe.
es funktioniert, aber die machen '<% 'Kommentare' machen%>' zeigt nichts .. Sie tun Warum? –
Sie sollten '<% = render 'Kommentare'%>' verwenden, nicht '<% render 'Kommentare'%>'. Beachten Sie das fehlende '=' Zeichen? – Dharam
Vielen Dank Ich vermisse die '=' –