1

「レシピ」テーブルと「材料」テーブルがあります。各レシピ「has_and_belong_to_many」の材料と各材料「has_and_belong_to_many」のレシピ。

材料ページへのリンクを追加したい:「この材料を含むすべてのレシピを表示する」。

材料コントローラーに次のコードを記述しました。

def recipes
    @ingredient = Ingredient.find(params[:id])
    @recipes = @ingredient.recipes
    respond_to do |format|
       format.html # index.html.erb
       format.json { render json: @recipes }
    end
end

私の問題は、「成分」ビューの下に「recipes.html.erb」ファイルがあることを期待していることです。このために新しいビューを作成したくありません。「レシピ」ビュー(recipes / index.html.erb)で使用しているのと同じコードを使用したいだけです。レールをこのビューに誘導するにはどうすればよいですか?

(私はrails 3.xを使用しています)

ありがとう、李

4

1 に答える 1

2

このような:

def recipes
    @ingredient = Ingredient.find(params[:id])
    @recipes = @ingredient.recipes
    respond_to do |format|
       format.html { render "recipes/index" }
       format.json { render json: @recipes }
    end
end

詳細については、Railsガイドをご覧ください:http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

于 2012-06-16T10:33:13.837 に答える