저는 레일에서 초보자이며 웹을 풍부하게하는 자습서를 따라갈 수 있도록 최선을 다하고 있습니다. 그래서 저는 세 개의 테이블을 가지고 있습니다.레일스 인덱스보기에서 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>
도움이 필요하시면 도움이됩니다.
:
그리고 다른 변화 이것은 실제로 일을하는 올바른 접근 방식과 같습니다. 완벽하게 작동합니다. –