【发布时间】:2011-06-01 14:27:27
【问题描述】:
我的 Rails 3 控制器中有这段代码:
def index
now = Time.now.utc.strftime("%m%d")
redirect_to :action => "show", :id => now
end
def show
begin
@date = Time.parse("12#{params[:id]}")
dbdate = params[:id]
rescue ArgumentError
@date = Time.now.utc
dbdate = @date.strftime("%m%d")
end
@date = @date.strftime("%B %d")
@events = Event.events_for_date(dbdate)
end
所以基本上 index 只是 show 的一个特殊版本,因此我希望它执行 show,呈现 show.html.erb 视图 - 但我确实 不 想要像 redirect_to 那样更改 URL .
我已经尝试过这种方法:
def index
now = Time.now.utc.strftime("%m%d")
params[:id] = now
show
render :action => "show"
end
现在,这行得通,但它闻起来很臭。
我是 Ruby 和 Rails 的新手,所以我想知道是否存在根本性错误,或者是否有更好的方法来做到这一点?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3