2016-05-15 60 views
0

我想創建一個簡單的表單,以便爲已發佈在食譜中的食譜創建新的評論。我呈現的配方顯示頁面上的形式審查,但它不斷給我同樣的錯誤:Rails簡單形式未定義的方法`model_name'爲零:NilClass

未定義的方法`MODEL_NAME」的零:NilClass

當我不呈現在部分新形式審查app/views/recipes/show.html.erb,但是我創建了文件app/views/reviews/new.html.erb,只有表單工作。我不明白爲什麼表單在我嘗試在show recipe頁面上呈現時無法正常工作。

這裏是我的代碼:

簡單的形式爲:

<%= simple_form_for(@review, url: recipe_reviews_path(@recipe)) do |f| %> 
    <%= f.error_notification %> 
    <%= f.input :content %> 
    <%= f.input :rating %> 
    <%= f.button :submit, class: "btn btn-success" %> 
<% end %> 

點評控制器:

class ReviewsController < ApplicationController 
    def new 
    @recipe = recipe.find(params[:recipe_id]) 
    @review = Review.new(review_params) 
    end 

    def create 
    @recipe = recipe.find(params[:recipe_id]) 
    @review = Review.new(review_params) 
    @review.recipe = @recipe 
    if @review.save 
     redirect_to recipe_path(@recipe) 
    else 
     render 'recipe/show' 
    end 
    end 

    private 

    def review_params 
    params.require(:review).permit(:content, :rating) 
    end 
end 

食譜控制器:

class RecipesController < ApplicationController 
    skip_before_action :authenticate_user! 
    def index 
    @recipes = Recipe.all 
    end 

    def show 
    @recipe = Recipe.find(params[:id]) 
    @user = User.find(@recipe.user_id) 
    @full_name = @recipe.user.first_name + " " + @recipe.user.last_name 
    end 
end 

配方顯示頁面:

<div class="review"> 
    <%= render 'review/new' %> 

    <% @recipe.reviews.each do |review| %> 
     <%= review.content %> 
     <%= review.rating %> 
    <% end %> 
</div> 

路線:

resources :recipes, only: [:index, :show] do 
    resources :reviews, only: [:create] 
    end 

型號:

class Recipe < ActiveRecord::Base 
    belongs_to :user 
    has_many :ingredients, dependent: :destroy 
    has_many :reviews, dependent: :destroy 

    validates :name, :summary, :course, :kitchen, :photo, :description, presence: true 
    validates :summary, length: { maximum: 30 } 
    mount_uploader :photo, PhotoUploader 

    accepts_nested_attributes_for :ingredients, reject_if: :all_blank, allow_destroy: true 
end 

模型回顧:

class Review < ActiveRecord::Base 
    belongs_to :recipe 

    validates :content, length: { minimum: 20 } 
    validates :rating, presence: true 
    validates_numericality_of :rating, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 5 
    validates :content, presence: true 
end 

任何人都可以看到這個問題?先謝謝你!

+0

你介意發佈模型和RecipesController嗎? –

+1

我會發布它們 – Seifer

+0

我相信你的問題在於** @ recipe = Recipe.find(params [:id])**沒有找到相應的配方,然後返回nil。然後,在視圖中,當你說**@recipe.reviews.each**時,它會抱怨你引用了一個無類的模型名稱(Review)。 –

回答

1

中的RecipesController的表演動作只需創建回顧模態的新實例 - :

@review = Review.new

這一切,我會努力。 :)

+0

它的工作表示感謝! – Seifer

+0

您最歡迎的@ Seifer –

0

您的RecipesController中是否有新方法?因爲你在簡單形式視圖中使用@recipe。您需要

def new 
@recipe = recipe.find(params[:recipe_id]) 
@review = Review.new(review_params) 
end 
+1

我爲了縮進而編輯。在理解代碼時,好的縮進功能非常有用。 –

+0

我已經在RecipesController中添加了新方法,但仍然出現錯誤。 :( – Seifer

+0

我認爲這是抱怨:** <%= simple_form_for(@review,url:recipe_reviews_path(@recipe))do | f |%> ** – Seifer

相關問題