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| %>
       <tr>
         <td><%= category.cat_name %></td>
         <td><%= category.cat_sub_name %></td>
         <td><%= category.supplier %></td>
         <td><%= link_to 'Show', category %></td>
         <td><%= link_to 'Edit', edit_category_path(category) %></td>
         <td><%= link_to 'Destroy', category, method: :delete, data: {
confirm: 'Are you sure?' } %></td>
       </tr>
     <% end %>


I have no idea how to this. pls help.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/de682fdaa1785bf9e4e3d86685668628%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Colin Law at Apr 12, 2016 at 7:45 am

    On 12 April 2016 at 08:17, Naveed Alam wrote:
    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 See below
    end

    and my index.html.erb view is

    <% @categories.each do |category| %>
    <tr>
    <td><%= category.cat_name %></td>
    <td><%= category.cat_sub_name %></td>
    <td><%= category.supplier %></td>
    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

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvj8f6i9CJbKhMSb-XqcEun%2Bi1_mHkfxpNV5FiUHztxHA%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Naveed Alam at Apr 12, 2016 at 7:53 am

    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

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bfd2ed876bc06e4819f265dcd1944bce%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Apr 12, 2016 at 9:53 am

    On 12 April 2016 at 08:52, Naveed Alam 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
    <td><%= category.supplier.company_name if category.supplier %></td>
    which will only attempt to determine the name if category.supplier is not nil

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLv86ZWqzLWK3E%3DfSE7WJ0_d4WwNjsfEtXCt6Ds87RCQ_w%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • Naveed Alam at Apr 12, 2016 at 1:43 pm

    Colin Law wrote in post #1182787:
    On 12 April 2016 at 08:52, Naveed Alam 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
    <td><%= category.supplier.company_name if category.supplier %></td>
    which will only attempt to determine the name if category.supplier is
    not nil

    Colin
    Sory none of them worked, pls chk my app,
    https://www.dropbox.com/s/siioqa8q8xkdf46/tPOS.zip?dl=0

    thanks.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f7a208acc016c2f5399a82b98c4bccab%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Apr 12, 2016 at 2:59 pm

    On 12 April 2016 at 14:43, Naveed Alam wrote:
    Colin Law wrote in post #1182787:
    On 12 April 2016 at 08:52, Naveed Alam 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
    <td><%= category.supplier.company_name if category.supplier %></td>
    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
    https://www.dropbox.com/s/siioqa8q8xkdf46/tPOS.zip?dl=0

    thanks.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f7a208acc016c2f5399a82b98c4bccab%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuCa0u_1r5FUhnn%3DaOcfok3J9rMr2QA32O39d%3DLtsrscA%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 12, '16 at 7:17a
activeApr 12, '16 at 2:59p
posts6
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Naveed Alam: 3 posts Colin Law: 3 posts

People

Translate

site design / logo © 2021 Grokbase