为什么PinsController中的ActiveRecord :: RecordNotFound会显示我的pin上的链接?

时间:2015-03-30 23:31:44

标签: html ruby-on-rails ruby activerecord

我的页脚上有一个链接。它适用于每个页面,除非您在查看引脚时单击它,它将返回以下错误:

PinsController#show中的ActiveRecord :: RecordNotFound 提取的来源(第61行):

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

    def correct_user

为什么只有当有人点击试图在引脚页面上查看页脚上的链接时才遇到此错误?

以下是更多相关细节:

pins_controller.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

这是terms.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'

如何解决此错误,以便人们在查看引脚时可以查看条款和条件?

非常感谢!

1 个答案:

答案 0 :(得分:1)

我可以建议你有href="terms"这样的术语的相对链接,所以点击它/pins/1 /pins/terms,所以要修复它,你应该看起来像{href="/terms" 1}}