我一直在努力实现以下目标......
目前,当我以用户身份创建联系人时,我对联系人的owner_id和user_id一直保持为nil。
以下是我到目前为止......我是否有任何明显的错误可以让他们快速找到解决方案?
模型
联系型号:
class Contact < ActiveRecord::Base
belongs_to :owner, :class_name => 'User'
belongs_to :user
validates :email, :presence => {:message => 'Email cannot be blank'}
end
用户模型:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :leadhooks
has_many :contacts, :foreign_key => 'owner_id'
# validates_formatting_of :website, using: :url
# validates_formatting_of :phone, using: :us_phone
end
控制器
contacts_controller.rb
class ContactsController < InheritedResources::Base
before_action :set_contact, only: [:show, :edit, :update, :destroy]
def index
@user = current_user
@contacts = @user.contacts.order("created_at DESC")
end
def show
end
def new
@contact = Contact.new
end
def edit
end
def create
@contact = Contact.new(contact_params)
respond_to do |format|
if @contact.save
format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
format.json { render :show, status: :created, location: @contact }
else
format.html { render :new }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @contact.update(contact_params)
format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
format.json { render :show, status: :ok, location: @contact }
else
format.html { render :edit }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
def destroy
@contact.destroy
respond_to do |format|
format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_contact
@contact = Contact.find(params[:id])
end
def contact_params
params.require(:contact).permit(:name, :user_id, :email, :owner_id)
end
end
模式
create_table "contacts", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "email"
t.integer "user_id"
t.integer "owner_id"
end
add_index "contacts", ["owner_id"], name: "index_contacts_on_owner_id", using: :btree
add_index "contacts", ["user_id"], name: "index_contacts_on_user_id", using: :btree
我一直在按照这里给出的答案......
How would you model contact list with self-reference and category?
但是,不知道为什么user_id和owner_id没有应用于联系人,以及如何不限制只为用户创建联系人(例如,允许通过表单创建联系人)由非用户在网页上提交)
非常感谢您的帮助。
答案 0 :(得分:0)
您有几个不同的顾虑。解决第一个问题:
1。 &#34;但是,不知道为什么user_id和owner_id没有应用于联系人&#34;
您似乎没有在控制器中构建关联。尝试这样的事情:
# controller
def new
@user = params[:user_id] # or @user = current_user, but you need to get a user
@contact = @user.contacts.build
end
def create
@user = params[:user_id] # or @user = current_user, but you need to get a user
@contact = @user.contacts.build(contact_params)
...
end
2。 &#34;如何不限制仅向用户创建联系人(例如,允许通过网页上的非用户提交的表单创建联系人)&#34;
如果我理解正确,联系人就是人,学生可以将多个紧急联系人存储在他们学校的档案中。如果这是真的,那么你在这里要求的东西是没有意义的。您始终希望将联系人的创建限制为用户。可以这样想:您只会在的联系人的上下文中关注联系人,因此需要所有者。想象一下,学校里有一个装满紧急联系人的盒子,卡片上没有学生名字(我可能在这里约会自己......)这些卡有什么用?
如果您确实希望它为零,则在创建新的Contact实例时,必须在控制器中允许nil。类似的东西:
@contact = Contact.new(user: current_user) # will return nil if empty