0

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  
Timmy Von Heiss
  • 2,160
  • 17
  • 39

2 Answers2

1

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

format.js { render :file => "/users/feed.js.erb"}
Community
  • 1
  • 1
Timmy Von Heiss
  • 2,160
  • 17
  • 39
  • 2
    You might want to add layout: false to that: format.js { render file: "/users/feed.js.erb", layout: false} – jacklin May 08 '19 at 15:56
0

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

Alex Kojin
  • 5,044
  • 2
  • 29
  • 31