私はこれらの2つのコントローラーをストーリーとカテゴリーについて紹介しています。今私は、ニュースを追加するときに既に作成されたカテゴリを選択して追加したいと思います。どのようにそれらを接続すればよいですか、selectタグを付けてnew.html.erbにすべてのカテゴリを表示する必要がありますか?2台のコントローラーを接続する方法
class StoriesController < ApplicationController
def index
@stories = Story.all
end
def show
@story = Story.find(params[:id])
end
def new
@story = Story.new
end
def create
@story = Story.new(params[:story])
if @story.save
redirect_to stories_path, :notice => "Your story was saved"
else
render "new"
end
end
def edit
@story = Story.find(params[:id])
end
def update
@story = Story.find(params[:id])
if @story.update_attributes(params[:story])
redirect_to stories_path, :notice => "Your story has been updated"
else
render "edit"
end
end
def destroy
@story = Story.find(params[:id])
@story.destroy
redirect_to stories_path, :notice => "Your story has been deleted"
end
end
class CategoriesController < ApplicationController
def index
@categories = Category.all
end
def new
@category = Category.new
end
def create
@category = Category.new(params[:category])
if @category.save
redirect_to categories_path, :notice => "Your category was saved"
else
render "new"
end
end
def edit
@category = Category.find(params[:id])
end
def update
@category = Category.find(params[:id])
if @category.update_attributes(params[:category])
redirect_to categories_path, :notice => "Your category has been updated"
else
render "edit"
end
end
def destroy
@category = Category.find(params[:id])
@category.destroy
redirect_to categories_path, :notice => "Your category has been deleted"
end
end
[この](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)が役に立つかもしれません。 –