I have this code in my Rails 3 controller:
我的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
So basically index is just a specialized version of show, hence I want it to execute show, render the show.html.erb view - but I do not want to change the URL like redirect_to does.
基本上index只是show的一个专门版本,因此我想让它执行show,渲染show。html。erb视图——但是我不想像redirect_to那样更改URL。
I have tried this approach:
我尝试过这种方法:
def index
now = Time.now.utc.strftime("%m%d")
params[:id] = now
show
render :action => "show"
end
Now, this works, but it just smells badly.
这行得通,但闻起来很糟糕。
I'm new to Ruby and Rails, so I just wonder if there is something fundamentally wrong or if there is a better way to do this?
我是Ruby和Rails的新手,所以我想知道是否存在根本的问题,或者是否有更好的方法来解决这个问题?
1 个解决方案
#1
4
What you're doing there appears to be fine. Writing it a bit differently might improve the smell a bit, but the differences are purely cosmetic.
你在那里做的一切似乎都很好。用不同的方式写出来可能会改善味道,但这些区别纯粹是修饰性的。
def index
now = Time.now.utc.strftime("%m%d")
params[:id] = now
render_show
end
def show
render_show
end
private
def render_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)
render :action => 'show'
end
#1
4
What you're doing there appears to be fine. Writing it a bit differently might improve the smell a bit, but the differences are purely cosmetic.
你在那里做的一切似乎都很好。用不同的方式写出来可能会改善味道,但这些区别纯粹是修饰性的。
def index
now = Time.now.utc.strftime("%m%d")
params[:id] = now
render_show
end
def show
render_show
end
private
def render_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)
render :action => 'show'
end