2013-09-08 13 views
13

Jestem trochę zagubiony. Otrzymuję błąd Rails 4: ActiveModel :: ForbiddenAttributesError. Rozumiem, że oznacza to, że muszę zezwalać na przekazywanie przedmiotów, co zrobiłem, ale muszę czegoś przegapić.ActiveModel :: ForbiddenAttributesError - Rails 4

Komentarze Kontroler:

class CommentsController < ApplicationController 
    def create 
     @post = Post.find(params[:post_id]) 
     @comment = @post.comments.create!(params[:comment]) 
     redirect_to @post 
    end 

    private 
     # Never trust parameters from the scary internet, only allow the white list through. 
     def comment_params 
      params.require(:comment).permit(:post_id, :comment, :body) 
     end 

end 

Tworzenie Komentarze Migration

class CreateComments < ActiveRecord::Migration 
    def change 
    create_table :comments do |t| 
     t.references :post 
     t.text :body 

     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :comments 
    end 
end 

Co ja tu brakuje? Daj mi znać, jeśli potrzebujesz zobaczyć inny kod.

Dzięki!

Odpowiedz

32

Zamiast

@comment = @post.comments.create!(params[:comment]) 

chcesz

@comment = @post.comments.create!(comment_params) 

Zrobiłeś całą ciężką pracę bez użycia dopuszczalne atrybuty!

+0

Bla! Wiedziałem, że to musi być trywialne. Dzięki! –

+1

Zadzwonił też do mnie z Rails 3, dzięki! – Zach

Powiązane problemy