用户只能编辑自己的帖子,因此我使用以下内容判断用户是否可以输入编辑表单:

  def edit
    @post = Load.find(:first, :conditions => { :user_id => session[:user_id], :id => params[:id]})
  rescue ActiveRecord::RecordNotFound
    flash[:notice] = "Wrong post it"
    redirect_to :action => 'index'
  end

但它不起作用,你知道我做错了什么吗?

推荐答案

如果要使用rescue语句,则需要以引发异常的方式使用find(),即传递要查找的id.

def edit
  @post = Load.scoped_by_user_id(session[:user_id]).find(params[:id])
rescue ActiveRecord::RecordNotFound
  flash[:notice] = "Wrong post it"
  redirect_to :action => 'index'
end

Ruby-on-rails相关问答推荐

Rails 7.1中的覆盖脚手架控制器

Rails文档的悲观锁定示例中的争用条件

如何从 Rails7.2 中的控制器获取名称空间?

如何将动态 Ruby 代码嵌入到 Slim 模板中的javascript部分?

使用brew 软件安装了 elasticsearch,但找不到配置文件

rspec 共享示例与共享上下文

没有图像时如何在回形针中显示隐藏图像

如何从 ActiveRecord 中的每个组中获取最新记录?

在 rake 任务期间关闭观察者的简单方法?

如何让空白复选框作为假传递给参数

设计记住我和会话

Rails 3.1 Sprockets 需要指令 - 有没有办法排除特定文件?

我可以在 twitter-bootstrap popover 数据内容中使用 html 标签吗?

何时使用 Helpers 而不是 Partials

Rails 3 返回 HTTP 406 Not Acceptable?

Rails 4,Capistrano 3.0.0,无法加载这样的文件 - 部署

如何在两个字段上对 ruby​​/rails 进行排序?

Rails: Sprockets::Rails::Helper::AssetNotPrecompiled 在开发中

将现有的 html.erb 转换为 Haml

rails路由中资源和资源之间的区别?