I want to pass a local variable that contains the origin to come on a specific page, this variable contains just a symbol with the value.
When I use this code it works perfect, the origin variable is accessible in the partial :
render :partial => "products", :collection => #products, :locals => {:origin => :gallery}
But when I use this code, the origin is not set and not accessible in the partial :
render #products, :locals => {:origin => :gallery}
What is the difference here? Is the second line of code not render the partial like the first line?
<%= render #products %>
Is indeed the shorthand syntax for rendering a partial. But with the shorthand syntax, Rails will ignore the ":locals" variable. There's more on this in the Rails Guides.
So if you want to pass extra options to the render, you have to specify ":partial => ...". If you want to know why this happens, you can take a look at the Rails source.
There's a good explanation here: Rails: confused about syntax for passing locals to partials
The short version is that you can just omit :locals in the second example:
render #products, :origin => :gallery
Related
I'm trying to put a file link with a partial render, something like this:
#Main haml file
= render :partial => 'file_upload', :locals => {:f => f, :file_download => 'residence_cert'}
#Inside the partial
%a{:href => #postulant_info[file_download]}
But this is making a link just with the file name, not the full path to the file. Then realized that #postulant_info[file_download] is just giving me a String with the file name, not the carrierwave object
-logger.debug #postulant_info.residence_cert #this is returning my carrierwave object -> 'DocumentUploader'
-logger.debug #postulant_info['residence_cert'] #but this one is just returning a String, the DB record with the file name
One solution could be put the url in the render as a local like = render :partial => 'file_upload', :locals => {:f => f, :file_download => 'residence_cert', :url => #postulant_info.residence_cert.url} but I think "should" be unnecessary when you have the attribute name inside the partial template.
Any thought would be appreciated. Thanks in advance
I'm extremely uncertain about what your problem exactly is, and what you're trying to achieve, but if you want to output a link to a file, in your partial then
%a{:href => #postulant_info.residence_cert.url}
is enough. #postulant_info.residence_cert will give you an instance of the uploader (DocumentUploader), and the #url method returns the complete url of the uploaded file
By the way, you should avoid using instance var in your partials, the only variables a partial should use are the it's been given.
At last I could achieve it with:
%a{:href => #postulant_info.send(file_download)}
in a current Rails 3.0.9 app of mine I had a few .js.erb templates that were using view_context in them so I could call fields_for on it during a ajax request. This was letting me build some nested attribute form fields via ajax. But upon upgrading to Rails 3.1 I'm getting the follow error:
ActionView::Template::Error (undefined local variable or method `view_context' for #<#:0x1057b9f70>):
Was this removed/deprecated recently? Is there another way I can build nested fields_for inputs without having the parent FormBuilder handy? It seems view_context is still available in the controller, but I was hoping to keep this markup generation in the View layer.
My .js.erb template looked like this
<% meal_item_fields = view_context.fields_for :meal_items, Meal.new.meal_items.new, :child_index => "new_meal_items" do |f|
render :partial => 'meal_items/meal_item_fields', :locals => {:meal_item_form => f}
end
%>
$("#meal-items").append("<%= escape_javascript(meal_item_fields) %>");
According to api docs it is deprecated in >= 3. Source of 3.0.9 returned self for view_context. I think if you were to try without view_context it would just work.
<% meal_item_fields = fields_for :meal_items, Meal.new.meal_items.new, :child_index => "new_meal_items" do |f|
render :partial => 'meal_items/meal_item_fields', :locals => {:meal_item_form => f}
end %>
$("#meal-items").append("<%= escape_javascript(meal_item_fields) %>");
You might want to add helper_method :view_context in your controller.
if am trying to pass a parameter through a link and assign it to a specific model. here is the code i'm working with:
<%= link_to ( image_tag("item.png", :size => "50*50", :border=> 0, :alt => "item", :title => "item"), {:action => 'initialize_order', :frame_id => 1 }) %>
right now :frame_id is getting passed through as:
{"frame_id"=>"1"}
i want the parameter to be assigned to the model :order, returning:
"order"=>{"frame_id"=>"1"}
i know the answer must be simple but i've searched for a while now.
try this in your url args:
{:action => 'initialize_order', :order => {:frame_id => 1 }}
alternatively, since you only have one parameter, just pass it as :id and assign it in the controller.
Even better, build your app using RESTful routes, with an orders_controller and an initialize method. If orders and frames have a parent-child relationship, then nest them in your resources. That way you can use all the path generators.
I have a partial with yeild blocks, which I set using content_for, when I render partial I am also trying to pass in locals. The locals never get picked up in the partial.
<%= render :partial => 'shared/block', :locals => { :cssclass => 'medium' } %>
When I try to access the partial using
<%= :cssclass %>
All I get is the bare "cssclass" as a string rather than the variable I have set it to. Can you not use content_for and render partial at the same time?
Update
I tried rendering a partial with locals, no yield or content_for and the the values I setup while rendering the partial are getting picked up. Is there something I am missing?
use <%= cssclass %> instead of symbol. locals set a variable, not a symbol, and when you output symbol it is just converted to string.
The navigation for my site is obviously stored in the application layout file. Part of that navigation is driven by the database. How to I render a partial in the layout and pass in the collection of objects for it to render?
EDIT:
I think my question revolves more around how to get data into the partial, is that done form the application controller or do I have to add the data in each action on each controller?
If you just wish to pass one object in then you can use the object key on render. The passed object will be accessible as an local variable of the same name as the partial. SO if the partial is called navigation the local variable will be navigation.
<%= render :partial => 'foo/navigation', :object => #my_collection
In the partial:
<% for obj in navigation %>
...
<% end %>
If you wish to pass multiple objects then you can use the locals key. The names of the local variables in your layout are then the keys of the passed hash.
<%= render :partial => 'foo/navigation', :locals => { :foo => 'Hello', :bar => 'World' }
In the partial:
<%= foo %>
<%= bar %>