【发布时间】:2014-05-10 10:35:07
【问题描述】:
我有一个超级简单的任务,不知道如何实现它。
主控制器如下所示:
class MainController < ApplicationController
def index
if render_text.include? "bar"
render :text => render_text, params: { :bar => true } # http://localhost:3000/main/index?bar=true
else
render :text => render_text # http://localhost:3000/main/index
end
end
private
def render_text
output = Page.find(params[:id])
end
end
根据数据库的输出,我想要么向 url 添加参数,要么只渲染文本而不需要任何额外的 url 参数。
所以网址最好是这样的:
http://localhost:3000/main/index?bar=true
【问题讨论】:
-
output = Page.find(params[:id]) 这将如何给出字符串 bar 的结果? Page.find(params[:id]) 的结果是什么,我想这会将找到的具有指定 ID 的记录分配给输出,并且方法 render_text 也不会返回任何内容。让我知道。
-
@Dave,Page 类型的所有记录都作为字符串返回。假设这是一个带有一些随机单词的文本
-
好的。方法 render_text 应该像这样返回输出 (return output = Page.find(params[:id])) 对吗?
标签: ruby-on-rails render