Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

ruby - RAILS–undefined method `model_name' for nil:NilClass

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 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change create method to

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...