这可能是一种简单的方式,我更希望通过练习来学习。
我在设计用户后面嵌套地址,同时将所有内容(用户除外)放在一个名为client的文件夹中,除索引和放大器外,它似乎都可以工作(新建/创建)。显示操作/屏幕会导致错误。
我尝试了很多变化,包括将客户端投入混合但我不认为这是问题。
它导致的错误(我错过了通关系吗?) NameError(未初始化的常量User :: Address)
的客户机/ address.rb
class Client::Address < ActiveRecord::Base
belongs_to :user
end
user.rb
class User < ActiveRecord::Base
# Devise Settings
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
# Model Relationships
has_many :addresses
end
client.rb
module Client
# Add Prefix to Table
def self.table_name_prefix
'client_'
end
end
的routes.rb
Rails.application.routes.draw do
# Security Devise Setup
devise_for :admins
devise_for :users
# Main Pages
root 'website/page#index'
# Client Sections
resources :users do
scope module: "client" do
root :to => 'dashboard#index'
resources :addresses
end
end
namespace :admin do
root :to => 'panel#index'
end
end
的客户机/ addresses_controller.rb
class Client::AddressesController < ApplicationController
before_action :set_address, only: [:show, :edit, :update, :destroy]
# Index
def index
user = User.find(params[:user_id])
@addresses = user.addresses
end
# GET /client/addresses/1
def show
end
# GET /client/addresses/new
def new
@address = Client::Address.new
end
# GET /client/addresses/1/edit
def edit
end
# POST /client/addresses
def create
@address = Client::Address.new(address_params)
if @address.save
redirect_to user_addresses_path, notice: 'Address was successfully created.'
else
render :new
end
end
# PATCH/PUT /client/addresses/1
def update
if @address.update(address_params)
redirect_to user_addresses_path, notice: 'Address was successfully updated.'
else
render :edit
end
end
# DELETE /client/addresses/1
def destroy
@address.destroy
redirect_to user_addresses_path, notice: 'Address was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_address
@address = Client::Address.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def address_params
params.require(:client_address).permit(:cacompanyname, :caaddressline1, :caaddressline2, :cacity, :caprovince, :capostalcode, :user_id)
end
end
index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Client Addresses</h1>
<table>
<thead>
<tr>
<th>Company Name</th>
<th>Address Line 1</th>
<th>Address Line 2</th>
<th>City</th>
<th>Province</th>
<th>PostalCode</th>
</tr>
</thead>
<tbody>
<% @addresses.each do |address| %>
<tr>
<td><%= address.cacompanyname %></td>
<td><%= address.caaddressline1 %></td>
<td><%= address.caaddressline2 %></td>
<td><%= address.cacity %></td>
<td><%= address.caprovince %></td>
<td><%= address.capostalcode %></td>
<td><%= link_to 'Show', user_address_path %></td>
<td><%= link_to 'Edit', edit_user_address_path(user_address) %></td>
<td><%= link_to 'Destroy', user_address, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'Add a New Address', new_user_address_path %>
答案 0 :(得分:1)
NameError(未初始化的常量User :: Address)
您需要明确定义class_name
以告诉 Rails 使用此功能,否则它会查找不是&#的类Address
39;那里。
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
# Model Relationships
has_many :addresses, class_name: 'Client::Address'
end