2016-04-24 3 views
0

Ich möchte eine Seite haben, die Links zu anderen Websites in meinem Projekt anzeigt. Ich habe links.html.erb in meinen Kundenansichten erstellt, aber wenn ich versuche, auf die Seite zuzugreifen, erhalte ich diesen Fehler.Ruby on Rails "Ich konnte keinen Kunden mit 'id' = links finden"

ActiveRecord::RecordNotFound in CustomersController#show

Couldn't find Customer with 'id'=links

Kunden Controller:

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 

Routen:

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' 

In Ansicht:

<% 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 %> 

Wer hilft mit was ist hier falsch? Vielen Dank.

+0

Können Sie den relevanten Seitencode der Ansicht posten? auch die Routen – Pavan

+0

Können Sie auch angeben, welche Zeile den Fehler auslöst? –

+0

Ich denke, es gibt ein Problem aus Ihrer Sicht. –

Antwort

1

get 'links' => 'customers#links', as: :link zum routes.rb hinzufügen und Ihren Link aktualisieren, wie:

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

Das funktioniert, danke! – Co2

1

ein bisschen eine Erweiterung auf die Antwort von Ganesh hier.

Wenn Sie dies tun:

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

Sie erstellen eine URL:

customers/links 

In Ihren Routen, das erste Spiel für customers/links ist customers/:id welche Routen zu customers/show mit params[:id] = 'links'. Sehen Sie die Guide, wenn Sie nicht verstehen, warum das wahr ist. Deshalb sollten Sie den Fehler, wenn es darum:

ActiveRecord::RecordNotFound in CustomersController#show

Couldn't find Customer with 'id'=links

Als Ganesh zu Recht ausführt, können Sie die Routen zwingen kann genau so, wie er sagt. Für mich ist es ein wenig übel, diese Links-Seite in den CustomerController zu legen und die Routen zu erzwingen. Aber das ist eine Designentscheidung, die auf dem Problem basiert, das Sie zu lösen versuchen.

+0

Das ist eine großartige Erklärung :) Danke @jvillian –