2016-10-17 2 views
1

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.

+0

Haben gefunden Sie eine Antwort hilfreich? – dnsh

Antwort

0

Dies ist wahrscheinlich ein verschachteltes Parameterproblem und wie Sie die starken Parameter definiert haben. Überprüfen Sie this answer für weitere Informationen.

Wenn Sie sehen müssen, was sich in den Parametern befindet, geben Sie eine pry Anweisung in die Steuerung ein und prüfen Sie sie dort.

Viel Glück!

0

Es gibt mehrere Dinge, die Sie betrachten sollten. Ich habe mehrere Änderungen an Ihrem Code vorgenommen. Geh sie durch.

In Ihren Videos/show.html.erb

<%= form_for Comment.new do |f| %> 
    <%= f.label(:body, "Comment") %> 
    <%= f.text_area(:body) %> 
    <%= f.hidden_field :video_id, :value => @video.id %> 
    <%= f.submit("Submit Comment") %> 
<% end %> 

senden video_id hidden_field verwenden. Senden Sie die aktuelle Benutzer-ID aus Sicherheitsgründen nicht. Wenn Sie die aktuelle Benutzer-ID aus dem Formular nehmen, kann der Endbenutzer Ihr Formular einfach in HTML bearbeiten und die Benutzer-ID einer anderen Person weitergeben. Dies ist eine der einfachsten und größten Schwachstellen.

In comments_controller.rb

def create 
    @comment = Comment.new(comment_params) 
    @comment.user = current_user # we are making sure that current_user is set to comment. 
    if @comment.save 
    flash[:notice] = "Comment successfully created" 
    redirect_to post_video_path(@comment.video.post, @comment.video) 
    else 
    @errors = @comment.errors.full_messages.join(', ') 
    flash[:notice] = @errors 
    render :'videos/show' 
    end 
end 

private 

    def comment_params 
     params.require(:comment).permit(:body, :user, :video_id) 
     # We are permitting video_id instead of video 
    end 
+0

Wäre das keine akzeptable Lösung? –

+0

erstellen def user = current_user video = Video.find (params [: video_id]) comment = Comment.new (comment_params) comment.user = user comment.video = Video post = Video.Post def comment_params params.require (: Kommentar) .permit ( : Körper, ) Ende –

+0

ich Ihren Code nicht funktionieren konnte. –

0

habe ich dies funktioniert, aber ich bin nicht sicher, ob dies die Sicherheitsbedenken Adressen, die ausgelöst @Dinesh.

comments_controller.rb

def create 
    @user = current_user 
    @video = Video.find(params[:video_id]) 
    @comment = Comment.new(comment_params) 
    @comment.user = @user 
    @comment.video = @video 
    @post = @video.post 
    if current_user == @video.user || current_user.admin 
    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 
    else 
    flash[:notice] = "Only OP or admin may comment" 
    render :"videos/show" 
    end 
end 

und

private 

def comment_params 
    params.require(:comment).permit(
    :body, 
) 
end 
Verwandte Themen