当我尝试编辑显示页面时,出现此错误消息(找不到带有'id'= edit的投资组合)。
def set_portfolio_item
@portfolio_item = Portfolio.find(params[:id])
end
我尝试通过在其中进行编辑来编辑ID,但仍然无法正常工作
class PortfoliosController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_portfolio_item, only: [:edit, :show, :update, :destroy]
layout 'portfolios'
access all: [:show, :index, :angular], user: {except: [:destroy, :new, :create, :update, :edit, :sort]},site_admin: :all
def index
@portfolio_items = Portfolio.by_position
@page_title = "Portfolios"
end
def sort
params[:order].each do |key, value|
Portfolio.find(value[:id]).update(position: value[:position])
end
render json: { status: "updated" }
end
def new
@portfolio_item = Portfolio.new
3.times { @portfolio_item.technologies.build }
end
def create
@portfolio_item = Portfolio.new(portfolio_params)
respond_to do |format|
if @portfolio_item.save
format.html { redirect_to portfolios_path, notice: 'Your portfolio item is now live.'}
else
format.html { render :new }
end
end
end
def edit
end
def update
respond_to do |format|
if @portfolio_item.update(portfolio_params)
format.html { redirect_to portfolios_path, notice: 'The record successfully updated.' }
else
format.html { render :edit }
end
end
end
def show
end
def destroy
#this going to perform the look up
# Destroy/delete the record
@portfolio_item.destroy
# Redirect
respond_to do |format|
format.html { redirect_to portfolios_url, notice: 'Post was removed.' }
end
end
private
def portfolio_params
params.require(:portfolio).permit(
:title,
:subtitle,
:body,
:main_image,
:thumb_image,
technologies_attributes: [:name]
)
end
def set_portfolio_item
@portfolio_item = Portfolio.find(params[:id])
end
end
当我点击http://localhost:3000/portfolios/edit
时,我希望可以转到节目编辑页面,但会得到
在PortfoliosController#show中的ActiveRecord :: RecordNotFound 找不到带有'id'= edit
的投资组合
这是我的表演路线
portfolios_show GET /portfolios/:id(.:format) portfolios#show
blog GET /blogs/:id(.:format) blogs#show
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format)
active_storage/disk#show
这是我的编辑路线
edit_user_password GET /password/edit(.:format) devise/passwords#edit
edit_user_registration GET /edit(.:format) devise/registrations#edit
edit_portfolio GET /portfolios/:id/edit(.:format) portfolios#edit
edit_blog GET /blogs/:id/edit(.:format) blogs#edit
答案 0 :(得分:1)
当我点击http://localhost:3000/portfolios/edit时,我会显示错误消息
ActiveRecord::RecordNotFound in PortfoliosController#show Couldn't find Portfolio with 'id'=edit
如果要编辑投资组合,则必须提供ID,例如http://localhost:3000/portfolios/1/edit。您当前的网址只能解释为Portfolios#show,其中“ edit”是路线的:id
部分。