2016-04-24 49 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 –