Ich versuche, einen Kommentar auf einer Video-Show-Seite zu erstellen. Wenn ich das Formular absende, gibt mir rails einen Hinweis: "Benutzer muss existieren, Video muss existieren". Ich bin mir nicht sicher, warum meine starken Params die Create-Methode nicht durchmachen.Rails starke Params nicht an Objekt übergeben, kann aufgrund fehlender gültiger Attribute keinen Kommentar erstellen
comments_controller.rb
def create
@user = current_user
@video = Video.find(params[:video_id])
@comment = Comment.new(comment_params)
@post = @video.post
if @comment.save
flash[:notice] = "Comment successfully created"
redirect_to post_video_path(@post, @video)
else
@errors = @comment.errors.full_messages.join(', ')
flash[:notice] = @errors
render :'videos/show'
end
private
def comment_params
params.require(:comment).permit(
:body,
:user,
:video
)
end
models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :video
validates :body, presence: true
end
Modelle/video.rb
class Video < ActiveRecord::Base
belongs_to :user
belongs_to :post
has_many :comments
end
views/Videos/show.html.erb
<%= @video.title %>
<%= content_tag(:iframe, nil, src: "//www.youtube.com/embed/#{@video.embed_id}") %>
<%= link_to "Delete Video", post_video_path(@post, @video), method: :delete %>
<%= link_to('Back', user_post_path(@user, @post)) %>
<h3>Comments</h3>
<%= form_for [@video, @comment] do |f| %>
<%= f.label(:body, "Comment") %>
<%= f.text_area(:body) %>
<%= f.submit("Submit Comment") %>
<% end %>
<% unless @comments.nil? %>
<% @comments.each do |comment| %>
<%= comment.body %>
<%= comment.user %>
<% end %>
<% end %>
Ich habe versucht, diese zu der Methode erstellen ...
@comment.user = current_user
@comment.video = @video
, dass der Kommentar erlaubt zu sparen, sondern stattdessen die comment.body anzuzeigen, ist es angezeigt, den Kommentar-Objekt. Es erklärt immer noch nicht, warum die starken Params nicht bestanden werden.
Haben gefunden Sie eine Antwort hilfreich? – dnsh