2011-12-24 99 views
0

我有這兩個控制器關於故事和類別,他們單獨工作。現在,我想在添加新聞時添加選擇已創建的類別。我應該如何連接它們以及如何使用select標籤在new.html.erb中顯示所有類別?如何連接兩個控制器

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

[This](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)可能會有幫助。 – 2011-12-24 22:06:49

回答

0

你可以嘗試用:

# 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 not story.id – user1107922 2011-12-25 00:25:02

+0

Inside' @ stories.each do | story |'use story.category.title'顯示每個故事的分類標題 – 2011-12-25 09:45:31

+0

不工作,我昨天試過,但是我有這個錯誤消息:未定義的方法'標題'爲零: NilClass – user1107922 2011-12-25 12:48:38

1

這不是你需要將控制器連接在一起。你真的只需要StoriesController來查詢類別列表,並將其放置到類變量中,以便視圖可以利用它。

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

我試過了,但是我遇到了類別問題。 <%= select(「story」,「category_id」,Category.all.collect {| p | [p.title,p.id]},{:include_blank => true})%> 我認爲我救了ID,但是當我想看到標題,我看到#<類別:0x000000035be318>,我不知道如何解決它 – user1107922 2011-12-24 23:46:48

0

嘿控制器ü可以訪問類的標題。創建一個分類標題數組而不是分類對象數組。

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

謝謝,我解決了這個問題,但我有另一個,當我想在index.html.erb查看所有類別不是id的標題的新聞。我試過這個:story.category.title 但我有錯誤消息:未定義的方法'標題'爲零:NilClass - – user1107922 2011-12-25 13:12:00