2016-04-24 9 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

はroutes.rbをへget 'links' => 'customers#links', as: :linkを追加して、あなたのリンクを更新:

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

それは、ありがとう! – Co2

1

ここガネーシュの答えの拡張のビットがあります。

あなたがこれを行うときは:あなたがへのURLを作成している

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

を:あなたのルートで

customers/links 

customers/linksための最初の試合はcustomers/:idparams[:id] = 'links'customers/showへのルートです。なぜこれが当てはまるのかわからない場合は、Guideを参照してください。それはあなたがエラーを取得している理由です:ガネーシャが正しく指摘

ActiveRecord::RecordNotFound in CustomersController#show

Couldn't find Customer with 'id'=links

として、あなたは彼が言うとおりにルートを強制することができます。私にとっては、このリンクページをCustomerControllerに配置し、ルートを強制するのはやや臭いです。しかし、それは実際にあなたが解決しようとしている問題に基づいた設計の決定です。

+0

これはすばらしい説明です:) Thanks @jvillian –

関連する問題