Rendering another Action without changing URL?

Posted by Michael Stum on Stack Overflow See other posts from Stack Overflow or by Michael Stum
Published on 2010-12-30T14:49:57Z Indexed on 2010/12/30 14:54 UTC
Read the original article Hit count: 256

I have this code in my Rails 3 controller:

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.

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?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby