最初使用“联系人”启动,正如预期的那样创建了一个正确允许CRUD的已填充联系人列表。然后使用设计用户的设备设置登录。以两个不同的用户身份登录时,每个用户只能看到自己的联系人,但目前用户看到所有相同的联系人。
任何帮助解决此问题都将受到赞赏?
(不确定是否应该从用户开始,然后创建联系人,感觉这是向后创建的。)
了解关联是用户' has_many:联系人'和联系人' belongs_to:users'一直在改变和飙升无济于事。
ContactsController
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
UsersController
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
答案 0 :(得分:1)
以两个不同的用户身份登录时,每个用户都应该只看到自己的用户 然而,目前用户看到所有相同的联系人
问题在于此行@contacts = Contact.all
。它包含所有联系人。由于您只想显示 current_user 的联系人,您只需将其显示在下方
@contacts = current_user.contacts
ActionView :: Template :: Error:PG :: UndefinedColumn:ERROR:列 contacts.user_id不存在第1行:选择1作为一个FROM “contacts”WHERE“contacts”。“user_id”...... ^:SELECT 1 AS one FROM “contacts”WHERE“contacts”。“user_id”= $ 1 LIMIT 1
contacts
表似乎没有user_id
列。创建一个新的迁移以添加相同的迁移并执行rake db:migrate