Timmy Von Heiss
Timmy Von Heiss

Reputation: 2218

Is it possible to specify what js.erb file I want to run?

If I have the following code, Rails is setup to automatically look in the views folder and find photos/feed.js.erb. But what I want to do is to tell it to run users/feed.js.erb instead.

PhotosController
def feed
    @title = "Favorites" 
    @user_feed_items = current_user.favorites.order('created_at desc').paginate(page: params[:page], per_page: 15)
      respond_to do |format|
        format.html { 
               render 'users/feed' }
        format.js 
      end 
  end  

Upvotes: 0

Views: 802

Answers (2)

Alex Kojin
Alex Kojin

Reputation: 5204

render accepts the full path (relative to app/views) of the template to render. So, you can just enter users/feed

render "users/feed"

Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the :template option.

render template: "users/feed"

http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

Upvotes: 0

Timmy Von Heiss
Timmy Von Heiss

Reputation: 2218

Rails: Render a .js.erb from another controller?

format.js { render :file => "/users/feed.js.erb"}

Upvotes: 1

Related Questions