Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...