render partial of another namespace - ruby-on-rails

I have a problem. I need to render objects using partials of another namespace.
render complain.target
it tryes to render partial from current namespace(current is admin)
Missing partial admin/bulletins/bulletin...
I dont need to render it from admin/..
I cant specify partial path like
render partial: '/bulletins/bulletin', locals: { bulletin: complain.target }
But it's polymorphic association, and different partial pathes are used.
Is it any way to do it?
Thanks in advance!

There seems to be no possible way to achieve this with a render complain.target call (Checked on Rails 5 source).
There is a config option for action_view to disable namespace prepending for partials, though:
Rails.application.config.action_view.prefix_partial_path_with_controller_namespace = false
EDIT
Today, I've used another solution:
When rendering Single-Table-Inheritance models into partials, one can pass the locals variable name based on the Rails model_name lookup, when calling the render partial:
<%= render partial: "admin/#{object.to_partial_path}",
locals: { object.model_name.element => object }
%>

You can use render "/#{complain.target.to_partial_path}"

Related

Rails 5: Rendering a partial to the application layout

Within views/layouts/application.html I have the line <%= yield %>. This line is ultimately replaced by views directly corresponding to controller actions without any problems.
In one of my controllers, I am trying to render a partial instead of the default behaviour:
def show
#service_groups = ServiceGroup.where(deleted_at: nil)
render partial: 'table', locals: {rows: #service_groups, headers: service_group_headers}
end
I'm using a partial in this way so that I can use the same basic table structure for various different database tables (across different controllers).
This render partial code doesn't seem to work with the <%= yield %> line in the application layout. The partial code is just rendered on its own without the surrounding layout.
Why is this?
How do I rectify the problem?
Please let me know if I should be handling this a different way.
Thanks.
(migrating from comments)
What if you created a show.html.erb for that controller and placed render :partial there? It should work.
Explanation: Partials are to be used from "big" views. So, they are not wrapped in the layout on purpose!

Best practice on including a rails partial that is shared among different resources?

Assuming a partial called dope, is the following the succinct way of including it from a view?
dealers.each do |dealer|
= render partial: 'common/dope', locals: {dealer: dealer}
IIRC you don't need the partial anymore so assuming you're doing this in haml, based on your syntax this would work
- dealers.each do |dealer|
= render 'common/dope', locals: {dealer: dealer}

Rendering just html with Rabl

I'm trying to figure out how to render just html with Rabl. I have an html partial without an object, so far all I can see are examples of partials with objects. If I try it without the object it just (obviously) throws an error.
The closest I got was:
node(:content) do
partial("api/v1/api/partials/tips")
end
Here is the documentation for Rabl.
UPDATE
I ended up just going with this below. Obvious right? For some reason when I tried it the first time it didn't work. So for sending only HTML in API responses, this works well.
def tips
render partial: "api/v1/api/partials/tips"
end
in a rabl view, for example show.v1.rabl you're able to render a view partial with the following (rails 4.1.2):
object #your_object
code :html do
context_scope.controller.render_to_string(
# from app/views/
partial: 'path/to/show.html.erb',
# locals (if needed)
locals: {
your_object: #your_object
}
)
end
This can be achieved by rendering an ERB template inside the Rabl node:
object #user
node(:html_content) do |user|
#user = user
template = Rails.root.to_s + '/app/views/api/users/show.html.erb'
ERB.new(File.read(template)).result(self.binding)
end

How do I define the controller for a partial view in rails?

How do I define the controller for a partial view in rails? I don't know how to expand the question further.
Any partial that I use between controllers is in the layouts directory. To use the partial, I then use the following code:
render :partial => 'layouts/myPartial'
Pretty sure you can just prepend to name of the partial:
In your view where you are using the partial:
render :partial => 'othercontroller/partialname'

Specify namespace when using "render" with an object in rails 3

So, you can do something like that in rails:
#features.each do |feature|
render feature
end
and it will look for a partial called _feature.html.erb in the views/features folder, based on the class name.
But what if features are in a cms namespace?
Is it possible to specify the namespace? Doing something like this (it doesnt work, obviously)
render [:cms, feature]
Thx
You'll have to be more explicit:
render :partial => '/cms/feature', :object => feature
This will render the 'app/views/cms/_feature.html.erb' partial with the object being 'feature'.

Resources