2

用户可以使用 URL 查看我的 webapp 中的特定条目。/entry/8, 例如。如果条目不存在,则会附加“找不到条目”@messages并呈现错误页面。

我想显示一些任意查询而不是空白页,但我想不出一个好方法来保持错误消息显示。在任意查询的控制器中还需要执行其他操作,因此我不能只复制查询和render :posts.

一些示例代码:

模块 MyApp::Controllers
  类 ComplexQuery < R '/query'
    定义得到
      @entries = Entries.all(:conditions => someComplexConditions)
      直到@entries.complex 够了吗?然后@entries.makeMoreComplex!结尾
    结尾
  结尾

  类 SingleEntry < R '/entry/(\d+)'
    定义获取(ID)
      @entries = Entries.find_all_by_id(id)
      除非@entries.nil?
        渲染:帖子
      别的
        @messages = [“该条目不存在。”]
        render :blank # 我想运行 Controllers::ComplexQuery,而不是渲染一个空白页面。
      结尾
    结尾
  结尾
结尾
4

2 回答 2

3

像这样的东西?

def get(id)
  @entries = Entries.find_all_by_id(id)
  unless @entries.nil?
    render :posts
  else
    r *MyApp.get(:ComplexQuery)
  end
end

请参阅Camping.method_missing

但我也建议将 ComplexQuery 移动到辅助方法中:

module MyApp::Helpers
  def complex_query(conditions); end
end

然后你可以complex_query(something)在 SingleEntry 和 ComplexQuery 中。

于 2010-07-25T11:54:16.800 回答
0

试试这个:

@entry = Entry.find_by_id(params[:id]) # returns nil when not found
if @entry.nil?
  flash[:notice] = "Entry not found"
  render :action => "recent" 
end
于 2010-04-11T20:25:34.653 回答