为什么PinsController#中有一个ActiveRecord::RecordNotFound显示我的引脚上的链接



我的页脚上有一个链接。它在每个页面上都有效,除非你在查看pin时点击它,它会返回以下错误:

ActiveRecord::RecordNotFound in PinsController#show提取来源(61号线附近):

Couldn't find Pin with 'id'=terms
private
    def set_pin
    @pin = Pin.find(params[:id])
    end
    def correct_user

为什么只有当有人在钉页上点击试图查看页脚上的链接时,它才会遇到这个错误?

以下是更多相关细节:

引脚_控制器.rb

class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy, :bid]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 9)
end
def bid
 Bid.where(user_id: current_user.id, pin_id: @pin.id).each do |bid|
  bid.cancel!
end
bid = Bid.new
bid.user = current_user
bid.pin = @pin
bid.price = params[:price].to_f
bid.quantity = params[:quantity] ? params[:quantity].to_i : 1
bid.save
render json: {
    success: true
}
   end
def show
@existing_bid = @pin.bid_for(current_user)
end
def new
@pin = current_user.pins.new
end
def edit
end
def create
@pin = current_user.pins.new(pin_params)
if @pin.save
  redirect_to @pin, notice: 'Listing was successfully created.'
else
  render action: 'new'
 end
end
def update
if @pin.update(pin_params)
  redirect_to @pin, notice: 'Pin was successfully updated.'
else
  render action: 'edit'
 end
end
def destroy
@pin.destroy
redirect_to pins_url, notice: 'Pin was successfully destroyed.'
end
private
  def set_pin
  @pin = Pin.find(params[:id])
 end
def correct_user
  @pin = current_user.pins.find_by(id: params[:id])
     redirect_to pins_path, notice: "Not authorized to edit this listing." if      @pin.nil?
end
def pin_params
  params.require(:pin).permit(:description, :image, :image2, :image3, :image4, :image5, :manufacturer, :model)
 end
 end

这是术语.html.erb

<center><h1>Company</h1></center>
<div class="thumbnail">
<div class="container-fluid">
<p><h3><b>Terms & Conditions</b></h3>
The terms and conditions will be defined in the listing.
</div>
</div>

Rake路线给出:

auction GET    /auction(.:format)             pages#auction
terms GET      /terms(.:format)               pages#terms
pins GET       /pins(.:format)                pins#index
new_pin GET    /pins/new(.:format)            pins#new
edit_pin GET   /pins/:id/edit(.:format)       pins#edit
pin GET        /pins/:id(.:format)            pins#show

routes.rb

get "about" => "pages#about" #creates about_path
get "contact" => "pages#contact" #creates contact_path
get "auction" => "pages#auction" #creates auction_path
get "terms" => "pages#terms" #creates terms_path
post 'send_mail', to: 'contact#send_mail'
get 'contact', to: 'contact#show'

我如何解决此错误,以便人们在已经查看pin时可以查看条款和条件?

非常感谢!

我可以建议您有一个到href="terms"这样的术语的相对链接,因此在/pins/1中单击它可以到达/pins/terms,所以要修复它,您应该使它看起来像href="/terms"

最新更新