浏览了其他类似的问题,但找不到答案。我正在使用Sinatra来构建应用程序。可以很好地创建所有内容,但是在编辑项目时出现错误,即找不到ID为的猫。当我尝试将:name更改为:id时,同样的问题。到目前为止,这是我这部分的代码:
user = Helpers.current_user(session)
if user.nil?
redirect to '/'
elsif params[:name].empty?
flash[:error] = 'Please name your cat'
redirect to '/create'
else
@cats = Cat.create(params)
@cats.user_id = session['user_id']
@cats.save
redirect to "allcats/#{params[:name]}"
end
end
get '/allcats/:name' do
"You've added #{params[:name]}!"
end
get '/allcats/:name/edit' do
if Helpers.is_logged_in?(session)
@cat = Cat.find(params[:name])
# @cats.user == Helpers.current_user(session)
erb :'/cats/edit'
# unless @cats.user == Helpers.current_user(session)
# redirect '/allcats'
# else
# flash[:error] = 'Must be logged in to edit cats'
# redirect to '/login'
end
end
end
如果必须使用id,请让我知道如何正确执行!当我尝试更改时不起作用。谢谢!