Hi,
Using rails 4, I have two models Supplier and Category
Category belongs_to Supplier
and Supplier has_many categories
now the suppliers table has fields, company_name, address, phone etc.
and id
and the categories has cat_name and supplier_id
in the categories index view I want to display all categories names
along with associated company_names.
in the categories_controller my index method is below.
def index
@categories = Category.all
#??? what to do to find company_name
end
and my index.html.erb view is
<% @categories.each do |category| %>
<%= category.cat_name %> |
<%= category.cat_sub_name %> |
<%= category.supplier %> |
<%= link_to ‘Show’, category %> |
<%= link_to ‘Edit’, edit_category_path(category) %> |
<%= link_to ‘Destroy’, category, method: :delete, data: {
confirm: ‘Are you sure?’ } %> |
<% end %>
I have no idea how to this. pls help.
On 12 April 2016 at 08:17, Naveed A. [email protected] wrote:
and id
#??? what to do to find company_name
See below
end
and my index.html.erb view is
<% @categories.each do |category| %>
<%= category.cat_name %>
<%= category.cat_sub_name %>
<%= category.supplier %>
category.supplier is the whole supplier record, so if you want the
name it is just
category.supplier.company_name
Such is the magic of Rails.
As a beginner I suggest you work right through a good tutorial in
order to get the basics or Rails. The one I suggest is
railstutorial.org (which is free to use online).
Colin
As a beginner I suggest you work right through a good tutorial in
order to get the basics or Rails. The one I suggest is
railstutorial.org (which is free to use online).
Colin
Thanks colin I already tried this but it give me the error:
undefined method `company_name’ for nil:NilClass
Colin L. wrote in post #1182787:
On 12 April 2016 at 08:52, Naveed A. [email protected] wrote:
As a beginner I suggest you work right through a good tutorial in
order to get the basics or Rails. The one I suggest is
railstutorial.org (which is free to use online).
Colin
Thanks colin I already tried this but it give me the error:
undefined method `company_name’ for nil:NilClass
Please quote the previous message when posting, this is a mailing list
not a forum (though you may be accessing it via a forum like
interface).
If the statement
category.supplier.company_name
gives the error undefined method `company_name’ for nil:NilClass then
that means that category.supplier is nil, or to put it another way, it
means that category does not have an associated supplier. You
probably need something like
<%= category.supplier.company_name if category.supplier %>
which will only attempt to determine the name if category.supplier is
not nil
Colin
Sory none of them worked, pls chk my app,
thanks.
On 12 April 2016 at 14:43, Naveed A. [email protected] wrote:
undefined method `company_name’ for nil:NilClass
probably need something like
<%= category.supplier.company_name if category.supplier %>
which will only attempt to determine the name if category.supplier is
not nil
Colin
Sory none of them worked, pls chk my app,
Just copy/paste the error here and the few lines of code around the
failure.
Colin
On 12 April 2016 at 08:52, Naveed A. [email protected] wrote:
As a beginner I suggest you work right through a good tutorial in
order to get the basics or Rails. The one I suggest is
railstutorial.org (which is free to use online).
Colin
Thanks colin I already tried this but it give me the error:
undefined method `company_name’ for nil:NilClass
Please quote the previous message when posting, this is a mailing list
not a forum (though you may be accessing it via a forum like
interface).
If the statement
category.supplier.company_name
gives the error undefined method `company_name’ for nil:NilClass then
that means that category.supplier is nil, or to put it another way, it
means that category does not have an associated supplier. You
probably need something like
<%= category.supplier.company_name if category.supplier %>
which will only attempt to determine the name if category.supplier is
not nil
Colin