gpt4 book ai didi

ruby-on-rails - 无法设置运营商

转载 作者:行者123 更新时间:2023-12-04 06:39:49 24 4
gpt4 key购买 nike

我正在尝试为我的新用户设置运营商,但是当我提交时,我收到了这个错误

Carrier(#37295244) expected, got String(#15496392)


<div class= "field">
<%= f.label :carrier %><br>
<%= f.collection_select(:carrier, Carrier.find(:all) ,:name, :name) %>
</div>

用户模型
class User < ActiveRecord::Base

acts_as_authentic
after_create :set_sub
after_create :set_universal
after_create :set_carrier

def set_sub
#self.roles << "subscriber"
self.roles_mask = 4
end

def set_universal
self.channels << Channel.find(1)
end

def set_carrier
self.carriers<< self.carrier_name
end

ROLES = %w[admin moderator subscriber]

#Each user can subscribe to many channels
has_and_belongs_to_many :channels

#Each user who is a moderator can moderate many channels
#has_many :channel_mods
has_and_belongs_to_many :modifies , :class_name => "Channel"

#Each user can receive many messages
has_and_belongs_to_many :messages

#Each user belongs to a carrier
belongs_to :carrier

#Filter users by role(s)
named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }

def roles
ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
end

def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
end

def role_symbols
roles.map do |role|
role.underscore.to_sym # NOT role.name.underscore.to_sym (role is a string)
end
end


end

载体模型
class Carrier < ActiveRecord::Base
has_many :users
end

用户 Controller
class UsersController < ApplicationController

filter_resource_access

# GET /users
# GET /users.xml
def index
@users = User.all

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end

# GET /users/1
# GET /users/1.xml
def show
#@user = User.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/new
# GET /users/new.xml
def new
#@user = User.new
@carriers = Carrier.find(:all)

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/1/edit
def edit
#@user = User.find(params[:id])
end

def create
#@user = User.new(params[:user])
#@user.channels << Channel.find(1)

respond_to do |format|
if @user.save
format.html { redirect_to(:channels, :notice => 'Registration successfully.') }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

def profile
@user = User.find(params[:id])
end



# PUT /users/1
# PUT /users/1.xml
def update
#@user = current_user

respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end

def delete
@user = User.find(params[:user_id])
@user.destroy
redirect_to :users
end

end

最佳答案

我还没有测试过你的代码,但乍一看我可以看到可能是错误的根源:

  def set_carrier
self.carriers<< self.carrier_name
end

您为 self.carriers 分配了carrier_name,可能是一个字符串,它应该包含一个 Carrier 对象数组。

关于ruby-on-rails - 无法设置运营商,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4395599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com