到目前爲止,我有user belongs_to :sites
和site has_many :users
。我在用戶架構中的站點架構和site_id
中有user_id
。在控制器中創建新站點時分配用戶site_id。
我試圖設置它,一旦用戶創建一個新的網站,比那個site_id被分配給該用戶。並且每個用戶只能創建一個站點。
我試過@user.site_id = Site.find(params[:id])
在sites_controller.rb中,但沒有運氣。
sites_controller.rb
def create
@site = Site.new(site_params)
@site.user_id = current_user.id
respond_to do |format|
if @site.save
format.html { redirect_to @site, notice: 'Site was successfully created.' }
format.json { render :show, status: :created, location: @site }
else
format.html { render :new }
format.json { render json: @site.errors, status: :unprocessable_entity }
end
end
end
registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
respond_to :json
def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
message = find_message(:signed_up)
flash[:notice] = message
sign_up(resource_name, resource)
if request.xhr?
return render :json => {:success => true, :data => {:message => message}}
else
respond_with resource, location: after_sign_up_path_for(resource)
end
else
message = find_message(:"signed_up_but_#{resource.inactive_message}")
expire_data_after_sign_in!
if request.xhr?
return render :json => {:success => true, :data => {:message => message}}
else
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
end
else
clean_up_passwords resource
messages = resource.errors.messages
if request.xhr?
return render :json => {:success => false, :data => {:message => messages}}
else
respond_with resource
end
end
end
def sign_up_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end
我認爲你想要註冊後爲什麼你在網站使用sites_controller.rb – Sunny