我希望有一个页面显示我项目中其他网站的链接。我在客户视图中创建了links.html.erb,但是当我尝试访问该页面时出现此错误。
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 :(得分:1)
将get 'links' => 'customers#links', as: :link
添加到routes.rb并将您的链接更新为:
<%= link_to image_tag("image1.jpg", size: "300x300"), link_path %>
答案 1 :(得分:1)
这是Ganesh的答案的一些扩展。
执行此操作时:
<%= link_to image_tag("image1.jpg", size: "300x300"), {:controller => 'customers', :action => "links" } %>
您正在创建一个网址:
customers/links
在您的路线中,customers/links
的第一个匹配为customers/:id
,其customers/show
路由params[:id] = 'links'
。如果您不明白为什么会这样,请参阅Guide。这就是您收到错误的原因:
CustomersController#show中的ActiveRecord :: RecordNotFound
找不到具有&#39; id&#39; =链接
的客户
正如Ganesh正确指出的那样,你可以像他说的那样严格强制路线。对我来说,将这个链接页面放在CustomerController中并强制路由是有点臭。但是,这确实是一个基于您试图解决的问题的设计决策。