2011-12-24 15 views
0

私はこれらの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 
+0

[この](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)が役に立つかもしれません。 –

答えて

0

あなたはと試みることができる:ねえコントローラにuがカテゴリのタイトルだけにアクセスすることができます

# app/controllers/stories_controller.rb 
def new 
    @categories = Category.all 
    @story = Story.new 
end 

# app/views/stories/_form.html.erb 
<%= form_for @story do |f| %> 
    #.... 
    <%= f.collection_select :category_id, @categories, :id, :title %> 
+0

はい、感謝しますがindex.html.erbでcategory.titleを表示する方法story.id – user1107922

+0

' @ story.each do | story | 'story.category.titleを使用して各ストーリーのカテゴリタイトルを表示します –

+0

昨日試しましたが、このエラーメッセージがあります:未定義メソッド' title 'for nil: NilClass – user1107922

1

コントローラーを一緒に接続する必要はありません。カテゴリリストを照会するには、実際にはStoriesControllerが必要です。ビューで利用できるように、クラス変数に配置してください。

class StoriesController < ApplicationController 
    ... 
    def new 
    @categories = Category.all 
    @story = Story.new 
    end 
+0

私は試みましたが、カテゴリに問題がありました。 %%私が保存したと思うのですが(%私はタイトルを見たいと思っているときに#<カテゴリ:0x000000035be318>を見て、それを修正する方法がわからない – user1107922

0

。カテゴリオブジェクトの配列ではなく、cattegoriesのタイトルの配列を作成します。

# app/controllers/stories_controller.rb 
    def new 
    @categories = Category.all.map(&:title) 
    @story = Story.new 
    end 
+0

を除いて' story.category.title 'を置くことができます。この問題は修正しましたが、index.html.erb IDのないカテゴリのタイトルごとにすべてのニュースを表示します。私はこれを試しました:story.category.title しかし、私はエラーメッセージがあります:未定義のメソッド 'title 'for nil:NilClass - – user1107922

関連する問題