【问题标题】:Rails Issues with association between User and ContactRails 用户和联系人之间的关联问题
【发布时间】:2016-08-30 08:06:27
【问题描述】:

最初从联系人开始,正如预期的那样,创建了一个正确允许 CRUD 的填充联系人列表。然后使用创建用户的设计设置登录。当以两个不同的用户身份登录时,每个用户都只能看到自己的联系人,但当前用户看到的都是相同的联系人。

如果有解决此问题的帮助,我们将不胜感激?

(不确定是否应该从User开始然后创建Contact,感觉就像是向后创建的。)

对关联的理解是用户 'has_many :contacts' 和联系人 'belongs_to :users' 一直在变化并无济于事。

联系人控制器

class ContactsController < ApplicationController

  before_action :contact, only: [ :show, :edit, :update, :destroy]   before_action :authenticate_user!


  def index
    @contacts = Contact.all   end

  def new
    @contact = Contact.new   end

  def create
    Contact.create(contact_params)
    redirect_to '/contacts'   end

  def show   end

  def edit   end

  def update
    @contact.update(contact_params)
    redirect_to '/contacts/' + "#{@contact[:id]}"   end


  def destroy
    @contact.destroy
    redirect_to '/contacts'   end

  private

  def contact_params
    params.require(:contact).permit(:firstname, :surname, :email, :phone, :image)   end``

  def contact
    @contact = Contact.find(params[:id])   end


end

用户控制器

class UsersController < ApplicationController

 end

model Contact
 class Contact < ActiveRecord::Base

   belongs_to :users

   has_attached_file :image, styles: {thumb: "100x100>"}  
 validates_attachment_content_type :image, content_type:
 /\Aimage\/.*\Z/

 end

模型用户

class User < ActiveRecord::Base   has_many :contacts, dependent: :destroy   devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable  end

indexhtml

<%if user_signed_in? %>   <%= link_to 'Log out', destroy_user_session_path, method: :delete %> <%end%>

<% if @contacts.any? %>   <% @contacts.each do |contact| %>   <%= link_to image_tag(contact.image.url(:thumb)), contact_path(contact) %> <h3><%= contact.firstname%> <%=contact.surname%></h3>   <%=contact.email%><br />   <%=contact.phone%>   <br />   <br />   <%end%> <%else%>   No contacts yet! <%end%> <br /> <br /> <%= link_to 'Add a contact', new_contact_path%>

显示 html

<p><%= image_tag @contact.image.url(:thumb) %></p> <p><%= @contact.firstname %> <%= @contact.surname %></p> <p><%= @contact.phone %></p> <p><%= @contact.email %></p>

<%= link_to 'Edit', edit_contact_path(@contact) %> <%= link_to 'Remove', contact_path(@contact), method: :delete %><br /><br /> <%= link_to 'Contacts', contacts_path %>

架构

ActiveRecord::Schema.define(version: 20160504125849) do

  # These are extensions that must be enabled in order to support this database   enable_extension "plpgsql"

  create_table "contacts", force: :cascade do |t|
    t.string   "firstname"
    t.string   "surname"
    t.string   "email"
    t.integer  "phone"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"   end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false   end

【问题讨论】:

    标签: ruby-on-rails associations


    【解决方案1】:

    当以两个不同的用户身份登录时,每个用户只能看到自己的 联系人,但当前用户看到所有相同的联系人

    问题出在@contacts = Contact.all 这一行。它拥有所有的联系人。由于您只想显示 current_user 的联系人,因此您只需将其置于下方

    @contacts = current_user.contacts
    

    ActionView::Template::Error: PG::UndefinedColumn: ERROR: column Contacts.user_id 不存在 LINE 1: SELECT 1 AS one FROM "联系人" WHERE "联系人"."user_id" ... ^ : SELECT 1 AS one FROM "contacts" WHERE "contacts"."user_id" = $1 LIMIT 1

    contacts 表似乎没有 user_id 列。创建一个新的迁移以添加相同的内容并执行rake db:migrate

    【讨论】:

    • 感谢您的回复。更新了代码但抛出了新错误Failure/Error: &lt;% if @contacts.any? %&gt; ActionView::Template::Error: PG::UndefinedColumn: ERROR: column contacts.user_id does not exist LINE 1: SELECT 1 AS one FROM "contacts" WHERE "contacts"."user_id" ... ^ : SELECT 1 AS one FROM "contacts" WHERE "contacts"."user_id" = $1 LIMIT 1
    • @blastoff 更新了我的答案
    • 有人有什么想法吗?已经检查了 psql 中的两个数据表(用户,联系人),每个表都包含一个 id 列。仍然收到错误Failure/Error: ActionView::Template::Error: PG::UndefinedColumn: ERROR: column contacts.user_id does not exist LINE 1: SELECT 1 AS one FROM "contacts" WHERE "contacts"."user_id" ... ^ : SELECT 1 AS one FROM "contacts" WHERE "contacts"."user_id" = $1 LIMIT 1
    • @blastoff 你没得到我的回答吗?您需要将user_id 添加到contacts 表中。
    • 嗨,是的,但认为 belongs_to 和 has_many 在模型上,它们会自动更新数据库(现在我知道它没有......并且必须调整表并重新迁移......doh !)。还必须将此 current_user.contacts.create(contact_params) 添加到 index.html 中的 create 方法中,然后才能成为 Contacts.create(contact_params) 并导致更多错误。 Tks for the revert Pavan!现在将尝试建立一对一的对话,这可能需要一段时间:)
    猜你喜欢
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2021-09-29
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多