I'm having trouble with rendering an action from different controller.
I want to render index action from TagsController
class TagsController < ApplicationController
def index
@tags = Tag.all
end
end
In tags/index.html.slim, it successfully returns objects from db.
= @tags
=>#<Tag::ActiveRecord_Relation:0x007ff7c65ca060>
So I tried this code in users/new.html.slim.
= render :template => "tags/index"
It shows nothing. Can anyone tell me what's wrong?
Anything helps, thanks!
You render templates from your controller, not from your views. Like this
class TagsController < ApplicationController
def index
@tags = Tag.all
render :template => "tags/index"
end
end
If you put,
render :template => "tags/index"
in your controller it will look for the directory and file like:
views/tags/index.html.erb
as you can render the data from @tags .
Solved it. I just used before_filter method.
before_filter :load_categories, only: [:index]
# your public controller methods
private
def load_categories
@categories = Category.all
end
I guess it's not the perfect way, which it's not calling an action from a different controller. I will try other answers as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.