我有一个Shop模型和一个User模型。逻辑是用户可以拥有一个商店,而商店可以具有一个与之关联的用户,因此我使用的是has_one: shop
关联。
但是在为新用户创建新商店时出现此错误
#<\ User:0x007f6f30659068>的未定义方法'shops'是什么意思?商店商店=
我不明白怎么了。我确定我一定做过一些愚蠢的事情,这是我的代码:
class ShopsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
def new
@shop = current_user.shop.build
end
def create
@shop = current_user.shops.build(shop_params)
@shop.user = current_user
respond_to do |format|
if @shop.save
format.html { redirect_to @shop, notice: 'Shop was successfully created.' }
format.json { render :show, status: :created, location: @shop }
else
format.html { render :new }
format.json { render json: @shop.errors, status: :unprocessable_entity }
end
end
end
private
def shop_params
params.require(:shop).permit(:name, :description, :imageshop, :location, :web, :email, :phone, :business_type, :category)
end
end
class Shop < ApplicationRecord
mount_uploader :imageshop, ImageUploader
belongs_to :user
end
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :products, dependent: :destroy
has_one :shop
end
答案 0 :(得分:0)
我认为您需要更改
hexString
到
@shop = current_user.shops.build(post_params)
这是因为您指定了一个用户可以拥有一个商店。
希望有帮助!