【发布时间】: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