2010-02-22 3 views
4

저는 레일에서 초보자이며 웹을 풍부하게하는 자습서를 따라갈 수 있도록 최선을 다하고 있습니다. 그래서 저는 세 개의 테이블을 가지고 있습니다.레일스 인덱스보기에서 ID 대신 선택된 이름 표시

class CreateAuthors < 
    ActiveRecord::Migration def self.up 
     create_table :authors do |t| 
      t.string :name 
      t.string :email 

      t.timestamps 
     end 

     end 

     def self.down 
     drop_table :authors end end 


class CreateTopics < 
ActiveRecord::Migration def self.up 
    create_table :topics do |t| 
     t.string :category 

     t.timestamps 
    end end 

    def self.down 
    drop_table :topics 
    end 
end 

이제 기사는 author_id를 참조하고 new.html.erb 및 edit.html.erb 지금

class CreateArticles < 
ActiveRecord::Migration def self.up 
    create_table :articles do |t| 
     t.string :title 
     t.integer :author_id 
     t.integer :topic_id 
     t.text :content 
     t.integer :status 

     t.timestamps 
    end end 

    def self.down 
    drop_table :articles end end 

을 topic_id 나는 주제의 기록을 얻을 수 collection_select를 사용하는 방법을 발견 및 저자.

<% form_for(@article) do |f| %> 
    <%= f.error_messages %> 

    <p> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title %> 
    </p> 
    <p> 
    <%= f.label :author_id %><br /> 
    <%= @authors =Author.find(:all, :order => 'name') 
    collection_select(:article,:author_id, @authors,:id,:name) %> 
    </p> 
    <p> 
    <%= f.label :topic_id %><br /> 
    <%= @topics = Topic.find(:all, :order => 'category') 
     collection_select(:article,:topic_id, @topics,:id,:category) %> 
    </p> 
    <p> 
    <%= f.label :content %><br /> 
    <%= f.text_area :content %> 
    </p> 
    <p> 
    <%= f.label :status %><br /> 
    <%= f.text_field :status %> 
    </p> 
    <p> 
    <%= f.submit 'Create' %> 
    </p> 
<% end %> 

<%= link_to 'Back', articles_path %> 

내보기에 어떻게 색인에서 이름을 반환하고 ID가 아닌보기를 표시합니까?

<td><%=h article.topic_id %></td> 
    <td><%=h article.title %></td> 
    <td><%=h article.author_id %></td> 
    <td><%=h article.status %></td> 

도움이 필요하시면 도움이됩니다.

답변

3

이 접근 방식은 Ruby on Rails 방식과는 다른 것으로, 컨트롤러 논리를 뷰에 혼합합니다. 보기에는 컨트롤러에 @authors = Author.find(:all, :order => 'name') 등이 있어야합니다.

@author = Author.find(@article.author_id); 

을 그리고 당신의보기에 당신은 작성자 이름 보여줄 것이다 :

마찬가지로, 당신의 컨트롤러에 당신은 할 것

<%=h @author.name %> 
+0

:

<td><%=h @article.title %></td> <td><%=h @article.author.name %></td> <td><%=h @article.status %></td> 

그리고 다른 변화 이것은 실제로 일을하는 올바른 접근 방식과 같습니다. 완벽하게 작동합니다. –

5

이 :

@authors =Author.find(:all, :order => 'name') 

이를 :

@topics = Topic.find(:all, :order => 'category') 

은 해당 작업 (newedit)의 컨트롤러에 있어야합니다.

귀하의 모델은 다음과 같아야합니다

# Article model 
belongs_to :author 
belogns_to :topic 

# Author model 
has_many :articles 

# Topic model 
has_many :articles 

을두고 당신이 이런 식으로 원하는 것을 할 수 있습니다 등 @article.topic.category, @author.articles.first.topic

+0

더 나은 설명입니다. 내 대답을 제거하십시오. 귀하의 기사 모델에 오타. – ghoppe

+0

@ghoppe 너의 대답도 좋았어. 특히'article.author.name'에 접근 할 때'@ author'에 저자를 적재하여 사용하는 편이 낫습니다. – klew

+0

보기를 표시하고 오타가 있음을 알게되었습니다. 굉장하고 고마워. –

관련 문제