2016-04-24 96 views
0

我想要一個顯示指向我項目中其他網站鏈接的頁面。我在我的客戶視圖中創建了links.html.erb,但是當我嘗試訪問該頁面時,出現此錯誤。Ruby on Rails「無法通過'id'= links找到客戶

ActiveRecord::RecordNotFound in CustomersController#show

Couldn't find Customer with 'id'=links

顧客控制器:

class CustomersController < ApplicationController 
    before_action :set_customer, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! 

    # GET /customers 
    # GET /customers.json 
    def index 
    @customers = Customer.all 
    @q = Tour.search(params[:q]) 
    @tours = @q.result.page(params[:page]).per(5) 
    @q.build_condition if @q.conditions.empty? 
    @q.build_sort if @q.sorts.empty? 
    end 

    def links 
    end 

    # GET /customers/1 
    # GET /customers/1.json 
    def show 
    @customers = Customer.all 
    end 

    def welcome 
    end 

    # GET /customers/new 
    def new 
    @customer = Customer.new 
    end 

    # GET /customers/1/edit 
    def edit 
    end 

    # POST /customers 
    # POST /customers.json 
    def create 
    @customer = Customer.new(customer_params) 

    respond_to do |format| 
     if @customer.save 
     format.html { redirect_to @customer, notice: 'Customer was successfully created.' } 
     format.json { render :show, status: :created, location: @customer } 
     else 
     format.html { render :new } 
     format.json { render json: @customer.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /customers/1 
    # PATCH/PUT /customers/1.json 
    def update 
    respond_to do |format| 
     if @customer.update(customer_params) 
     format.html { redirect_to @customer, notice: 'Customer was successfully updated.' } 
     format.json { render :show, status: :ok, location: @customer } 
     else 
     format.html { render :edit } 
     format.json { render json: @customer.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /customers/1 
    # DELETE /customers/1.json 
    def destroy 
    @customer.destroy 
    respond_to do |format| 
     format.html { redirect_to customers_url, notice: 'Customer record successfully deleted' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_customer 
     @customer = Customer.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def customer_params 
     params.require(:customer).permit(:name, :address, :telephone_no, :ticket_number) 
    end 
end 

途徑:

Rails.application.routes.draw do 
    devise_for :admin_users, ActiveAdmin::Devise.config 
    ActiveAdmin.routes(self) 
    resources :customers 
    resources :tours 
    devise_for :users 


    root 'customers#welcome' 

鑑於:

<% if current_user.customer? %> 
<div class="col-sm-4"> 
<%= link_to image_tag("image1.jpg", size: "300x300"), {:controller => 'customers', :action => "links" } %> 

<h3>Links</H3> 
</div> 
<% end %> 

任何人都可以幫助解決這裏出現的問題嗎?謝謝。

+0

你能發表相關的視圖頁面代碼嗎?還有路線 – Pavan

+0

另外,你能指出哪一行觸發了錯誤嗎? –

+0

我認爲你的看法存在問題。 –

回答

1

添加get 'links' => 'customers#links', as: :link到routes.rb中和更新的鏈接爲:

<%= link_to image_tag("image1.jpg", size: "300x300"), link_path %> 
+0

這工作,謝謝! – Co2

1

這裏有點上Ganesh的答案的擴展。

當你這樣做:

<%= link_to image_tag("image1.jpg", size: "300x300"), {:controller => 'customers', :action => "links" } %> 

你正在創建一個URL:

customers/links 

在你的路線,爲customers/links的第一場比賽是customers/:id該路線customers/showparams[:id] = 'links'。如果您不明白爲什麼這是真的,請參閱Guide。這就是爲什麼你得到錯誤:

ActiveRecord::RecordNotFound in CustomersController#show

Couldn't find Customer with 'id'=links

由於Ganesh神正確地指出,你可以強迫路線完全一樣,他說。對我而言,將此鏈接頁面放入CustomerController並強制路線是有點臭的。但是,這實際上是一個基於您試圖解決的問題的設計決策。

+0

這是很好的解釋:)謝謝@ jvillian –