RAILS–undefined method `model_name' for nil:NilClass

时间:2015-08-07 02:43:32

标签: ruby-on-rails ruby

i cant wrap my head around this problem, need some help :D. I have one Model name "Recipe", defined Rest and so on... but i wanted to add 1 custom method to the 7 actions of REST. In this case, after save a Recipe it should redirect_to the createingredient action (with the :id in the params hash)

In the createingredient action, a variable should be define (@recipe) by the params[:id]. The prams[:id] exits, but somehow the view could not access to the @recipe

 def new
  @recipe = Recipe.new
end

def create
    @recipe = Recipe.new(params_value)
    if @recipe.save
      redirect_to action: "newingredient",id: @recipe.id, notice: "Recipe succesfully created, time to add Ingredients"
    else
      render "new"
    end
end

the custommethode

  def newingredient
@recipe = Recipe.find(params[:id]) end

Thank you very much !

1 个答案:

答案 0 :(得分:1)

create方法更改为

def create
  @recipe = Recipe.new(params_value)
  if @recipe.save
    redirect_to newingredient_recipe_path(@recipe), notice: "Recipe succesfully created, time to add Ingredients"
  else
    render "new"
  end
end

config/routes.rb

resources :recipes do
  member do
    get :newingredient
  end
end