2015-10-20 56 views
0

這可能是一種簡單的方法,這是我想通過實踐學習更多。設計嵌套地址(一切似乎工作,除了索引/顯示)

我在將所有內容(用戶除外)放在名爲client的文件夾後嵌套地址,它似乎都工作(新建/創建),除了索引&顯示動作/屏幕導致錯誤。

我曾嘗試過很多變體,包括將客戶端與混音混合使用,但我不認爲這是問題所在。

錯誤,它會導致(我失去了一個通過關係?) NameError(未初始化的常數用戶::地址)

客戶端/ 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 %> 
+1

什麼是錯誤?請在問題中發佈。 – Pavan

+0

哦對不起,我知道我忘了給我一分鐘。 – bradpotts

+0

在頂部發布錯誤和添加模型。 – bradpotts

回答

1

NameError(未初始化常數用戶::地址)

您需要明確定義class_name才能告訴Rails要使用此功能,否則它將查找Address不在其中的類別。

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable 
    # Model Relationships 
    has_many :addresses, class_name: 'Client::Address' 
end 
+1

謝謝。只要給我一秒鐘來測試它,我會給你信用。 – bradpotts