質問

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

役に立ちましたか?

解決

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top