Arun Satyarth
Arun Satyarth

Reputation: 454

In Rails how can I handle both Ajax request and normal HTML request in same action?

My websites has a tabbed format where I populate only 2 out of 5 tabs in users home page. The rest of the tabs are populated with Ajax GET request when user clicks on the tab. Suppose I have an action to populate the album tab using Ajax upon tab click

def show_album
    #code to retrieve album
    respond_to do |format|
        format.js {render :file => "path_offile.js.erb"}
    end
end

Inside the js file I use a jquery selector to show a partial in the empty tab-content space.

$('#albumtab').html("<%= j (render  :partial => 'profiles/show_my_album' )  %>");

The problem is when I want to change the URL using history.pushState to "/show_album" so that when people reload the page they would stay on the album tab(sort of how it works on facebook).

So when users reload the page, they come to the same action, but I cannot return a js.erb file from here as there is no existing page where I can add a partial.

So what and how should I return from this action so that users can see the album page. Will I need to create a separate action for normal http requests?

Upvotes: 0

Views: 355

Answers (1)

ruby_newbie
ruby_newbie

Reputation: 3285

def show_album
    #code to retrieve album
    respond_to do |format|
        format.js {render :file => "path_offile.js.erb"}
        format.html{ <whatever stuff you want to happen here>}
    end
end

Upvotes: 1

Related Questions