2015-10-20 59 views
1

我正在研究Rails 4應用程序並試圖實現類別和配方之間的HABTM關係。我收到上述錯誤。我跟着一個在視頻中工作過的railscast,但沒有爲我工作。我添加了強大的參數。ActionController :: RecipesController中的InvalidAuthenticityToken#使用HABTM協會時創建

模式:

create_table "categories", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "categories_recipes", id: false, force: :cascade do |t| 
    t.integer "category_id" 
    t.integer "recipe_id" 
    end 

    create_table "recipes", force: :cascade do |t| 
    t.string "name" 
    t.string "source" 
    t.string "servings" 
    t.string "comment" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

end 

Models: 
class Category < ActiveRecord::Base 
    has_and_belongs_to_many :recipes 

    validates :name, :presence => true 
end 

class Recipe < ActiveRecord::Base 
    has_and_belongs_to_many :categories 

    validates :name, :presence => true 
    validates :source, :presence => true 
    validates :servings, :presence => true 
    validates :comment, :presence => true 
end 

形式:

<%= form_for @recipe do |f| %> 
    <p> 
    <%= f.label :name %> 
    <%= f.text_field :name %> 
    </p> 
    <p> 
    <%= f.label :source %> 
    <%= f.text_field :source %> 
    </p> 
    <p> 
    <%= f.label :servings %> 
    <%= f.text_field :servings %> 
    </p> 
    <p> 
    <%= f.label :comment %> 
    <%= f.text_field :comment %> 
    </p> 

    <%= hidden_field_tag 'recipe[category_ids]', nil %> 
    <% Category.all.each do |category| %> 
    <p> 
     <%= check_box_tag 'recipe[category_ids][]', category.id, @recipe.category_ids.include?(category.id) %> 
     <%= category.name %> 
    </p> 
    <% end %> 

    <%= f.submit %> 
<% end %> 

recipes_controller.rb:

def create 
    @recipe = Recipe.new(recipe_params) 
    if @recipe.save 
     redirect_to recipes_path 
    else 
     render :new 
    end 
    end 

    private 
    def recipe_params 
    params.require(:recipe).permit(:name, :source, :servings, :comment, {category_ids:[]}) 
    end 

layout.html.erb

<!DOCTYPE html> 
<html> 
<head> 
    <title>Friends</title> 
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> 
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 
    <%= csrf_meta_tags %> 
</head> 
<body> 

<%= yield %> 

</body> 
</html> 
+1

你檢查出這個問題? http://stackoverflow.com/questions/3364492/actioncontrollerinvalidauthenticitytoken – wspurgin

+1

嘗試從強大的參數中去除'category_ids:[]'周圍的{大括號}。 –

+0

我試過了,但沒有改變任何東西。不管怎麼說,還是要謝謝你。 – jkfairless

回答

0

錯誤與CSRF (Cross-Site Request Forgery)

該做的基本上意味着你要麼沒有得到過你的csrf_meta_tags meta標籤在佈局設置,或者你有你的控制器本身的問題。

使用帶有CSRF元標記的form_for輔助器(您就是這樣)應該可以解決這個問題。


順便說一句,你不妨使用多申報爲您validates

#app/models/recipe.rb 
class Recipe < ActiveRecord::Base 
    validates :name, :source, :servings, :comment, presence: true 
end 

......以及一些循環爲表單:

<%= form_for @recipe do |f| %> 
    <% %i(name source servings comment).each do |attr| %> 
    <%= f.label attr %> 
    <%= f.text_field attr %> 
    <% end %> 
    <%= f.collection_check_boxes :category_ids, Category.all, :id, :name %> 
    <%= f.submit %> 
<% end %> 

您還不該不會使用<p>標籤進行格式化 - 使用CSS

可以使用margin財產CSS實現的結果你想要的:

form input { 
    margin: 10px 0; 
} 
+1

我的佈局中有scrf_meta_tags。往上看。無法看到我的控制器的任何問題。我是一個新手,所以我不完全理解這一切,但願意。感謝您的提示!我真的很喜歡通過形式循環! – jkfairless

相關問題