Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show selected name instead of ID in rails index view

So I'm a newbie at rails and I'm trying as best as I can follow tutorials that abound the web. So I have three tables.

  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

Now the Articles reference the author_id and topic_id

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

Now for the new.html.erb and edit.html.erb I found out how to use collection_select to get the records from the topics and authors.

<% 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 %>

Now for my view how do I return the names in the index and show view and not the id?

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

Any help would be grateful.

like image 426
DT. Avatar asked Feb 28 '23 12:02

DT.


2 Answers

This:

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

and this:

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

should be in your controller in corresponding actions (new and edit).

Your models should look like this:

# Article model
belongs_to :author
belogns_to :topic

# Author model
has_many :articles

# Topic model
has_many :articles

With this you can do what you want in this way:

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

And any other variations: @article.topic.category, @author.articles.first.topic etc.

like image 58
klew Avatar answered Mar 11 '23 10:03

klew


This approach isn't quite the Ruby on Rails way of doing things, you're mixing controller logic into your View. You should have @authors = Author.find(:all, :order => 'name') etc. in your controller, not your View.

Similarly, in your Controller you would have:

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

And in your View you would show the author name:

<%=h @author.name %>
like image 21
ghoppe Avatar answered Mar 11 '23 09:03

ghoppe