0

I have USERS which have LISTINGS and a SHOP.

I just created the Shop Controller and in my def show action i'm messing things Up.

When a user creates a Shop it passes a user_id to the shop so every shop belongs to a User.

class AddUserIdToShops < ActiveRecord::Migration
  def change
    add_column :shops, :user_id, :integer
    add_index :shops, :user_id
  end
end

But on the Show Page i can not call @user.name for example because rails don't know what user i mean.

How can i set up my show action properly so the i can call @user ?

Thank you

Mini John
  • 7,855
  • 9
  • 59
  • 108

1 Answers1

1

Since shop belong_to a user. So in the show page of the shop. You can get the user by using the associations simply like this.

views/shops/show.html.erb

@shop.user.name
Bot
  • 1,001
  • 2
  • 13
  • 32
  • Well another Great Guy Helped me out, but this is somewhat what i needed so im checking your answer :) Thanks. – Mini John Aug 18 '13 at 23:57
  • 1
    You are welcome. Btw I see here that you have created a Shop class as well now. So my suggestion to you in last answer that naming ShopController is fine in no more valid. It needs to be ShopsController. Check this out for more info. http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails – Bot Aug 19 '13 at 00:05