layouts vs. shared vs. helpers folders (and when to use partials) - ruby-on-rails

I'm trying to build out an app and I started getting confused when to use a partial as opposed to simply refactoring code. In other words, when should I use 'render' and 'render partial: ........"
And if I put something in the "shared" folder under "views" does that make it a partial? Not sure when to use these different folders. Thanks a whole bunch!

Normally you use 'render' for just move some html codes, like "footer".
If you want the partial with its own layout or pass variables in it. We will use 'render :partial'
<%= render "footer" %> # Basic usage
<%= render "shared/footer" %> # _footer.html will be placed in "shared/_footer.html"
<%= render :partial => "sidebar", :layout => "sidebar_layout" %>
# It will using "_sidebar_layout" as a layout template for "_sidebar.html"
<%= render :partial => "form", :locals => { :post => #post } %>
# Passing #post variable as post in form partial
Reference: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

Related

How to use single html.erb file for multiple actions

I want to create multiple function but each functions render to its data to single file as like simple php, is possible with ruby and rails. I tried to found this but search results are not as per my point of view.
Partials will help you:
Create view than you want show in other views, name it _your_view.html.erb
Include it in other views:
<%= render :partial => 'path/to/your/partial/your_view' %>
Path can be skipped if your partial is in /app/views root folder, not in subfolders.
Don't forget remove first _ symbol from partial name while including (file is _your_view.html.erb and including is your_view).
Also you can pass parameters into partial:
<%= render :partial => 'path/to/your/partial/your_view', :locals => {:param1 => 'value1' } %>
and use them in partial:
<% value = local_assigns[:param1] %>
<%= text_field_tag :param1, :value => value %>
Yes, this is possible. You should look at using custom View helpers. For start, look at these links:
http://www.rails-dev.com/custom-view-helpers-in-rails-4
http://api.rubyonrails.org/classes/ActionView/Helpers.html

Pass Parameters in render - Rails 3

I've seen a couple questions on this but haven't been able to solve it...
I'm trying to pass a parameter while rendering a partial (similar to domainname.com/memory_books/new?fbookupload=yes)
Right now, I use this line:
<%= render :partial => '/memory_books/new', :fbookupload => "yes" %>
and in the partial, I have tried to get the content of fbookupload by using:
<%= fbookupload %>
which gives an "undefined local variable" error and
<%= params.inspect %>
which does not show fbookupload as a parameter.
How can I have the partial pass along the parameter :fbookupload?
Thank you.
UPDATE:
Could it have anything to do with the fact that I'm rendering this within a render?
i.e. the page (/fbookphotos/show) that has
<%= render :partial => '/memory_books/new', :fbookupload => "yes" %>
is being rendered by another page with (posts/show) via:
<%= render :partial => '/fbookphotos/show' %>
so I'm rendering this within a render.
try this:
<%= render :partial => '/memory_books/new', :locals => {:fbookupload => "yes"} %>
Taking it out of the comments for posterity. This syntax is correct:
render '/memory_books/new', fbookupload: "yes"
But if there is a reference to rendering the same partial without specifying the local variables, e.g.
render '/memory_books/new'
then fbookupload variable becomes unavailable. The same applies to multiple local variables, e.g.
render 'my_partial', var1: 'qq', var2: 'qqq'
will work if only occurs once. But if there is something like that somewhere else in the code
render 'my_partial', var1: 'qq'
then the var2 will become unavailable. Go figure ...
To do it your way:
In the main view:
<% fbookupload = "yes" %>
<%= render :partial => '/memory_books/new', :locals => {:fbookupload => fbookupload} %>
And in the partial:
<%= fbookupload %>
2nd option:
Ideally in the controller, otherwise in the view, define an instance variable: #fbookupload = "yes". Then it is available everywhere. The partial will then be : <%= #fbookupload %>
Params is just request parameter, so if u want to pass it in params u have to add it to your url ?fbookupload=yes or assign it params[:fbookupload] = "yes", but i don't think that is a good idea.
But if u need to use params[:fbookupload]', u can replace it withparams[:fbookupload] || fbookupload', and pass fbookupload in locals hash for partial.
render can be called with or without the partial param, and there seems to be some confusion around the differences between these two forms.
The following two are equivalent:
<%= render "my_partial', my_param: true %>
and:
<%= render partial: "my_partial', locals: { my_param: true } %>
The first is a shorthand that allows you to omit partial:. With this shorthand, local variables are also not nested under locals:. This is explained well in the documentation (see 'Rendering the default case').
In the two cases above, you would access my_param in the partial directly with my_param.
One other source of confusion is that if you render the partial somewhere without passing my_param, then the partial will fail when it tries to access it. To get around this, you can access the local with local_assigns[:my_param] instead of my_param, which will give you nil if the param is not defined instead of erroring, as described in this documentation. Another alternative is to use defined?(my_param) before accessing it.

Javascript inside view in rails 3

I need to put some javascript inside a view. Basically I am having a play with the Recurly.js library.
Here is an example form: http://pastie.org/3142536
I have tried adding the JS to a partial: filename _recurly.js.erb
<%= render :partial => 'recurly.js', :locals => { :company => #company } %>
But its just outputting the JS to the page.
Is there a better way of doing this passing vars to JS for output and how can I get it to render the JS ?
Hope you can advise.
You need to use the function escape_javascript.
so your code should be:
<%= escape_javascript(render(:partial => 'recurly.js', :locals => { :company => #company }) %>
have you tried:
<%= raw(render :partial => 'recurly.js', ...) %>
Normally ERB will escape the output. Not sure if this is a good solution in the long run, even if it works, though.
why don't you use normal way of including javascript in your ERB view.
<%= javascript_include_tag "filename_from_public_javascripts_folder" %>

render the html contents of a controller's action inside a div

I would like to render the contents of an action (e.g new_sub_batch) inside a div.
I tried
<div id="newBatch">
<%= render :template => 'new_sub_batch.html.erb' %>
</div>
but nothing is displayed.
I even tried <%= render :action => 'new_sub_batch' %>..still nothing.
Any suggestion??
Thanks a lot
What you want are partials. Distil the common markup that both views will use into a single file, and prefix its name with an underscore. Then call render :partial => 'filename', where filename is the name of the partial without the underscore.
In your case, the code you pull out of new_sub_batch.html.erb might go in a _batch.html.erb partial, in the same directory as your other sub_batch views. You would render this partial with:
render :partial => 'batch'
In Rails3, you can simply use render 'batch'.
If you want to pass a variable to the partial, you can do so via :locals. Assuming you have a #sub_batch variable you want to pass, your call would look something like this:
render :partial => 'batch', :locals => { :sub_batch => #sub_batch }
While this doesn't strictly answer your question, I believe within the ruby-on-rails tag it's more important to explain the Rails Way, rather than help you do it the wrong way.
Try file render:
<div id="newBatch">
<%= render :file => 'directory/new_sub_batch.html.erb' %>
</div>

Rails AJAX: My partial needs a FormBuilder instance

So I've got a form in my Rails app which uses a custom FormBuilder to give me some custom field tags
<% form_for :staff_member, #staff_member, :builder => MyFormBuilder do |f| %>
[...]
<%= render :partial => "staff_members/forms/personal_details", :locals => {:f => f, :skill_groups => #skill_groups, :staff_member => #staff_member} %>
[...]
<% end %>
Now, this partial is in an area of the form which gets replaces by an AJAX callback. What I end up doing from the controller in response to the AJAX request is:
render :partial => "staff_members/forms/personal_details", :locals => {:skill_groups => #skill_groups, :staff_member => #staff_member}
However, if I do that then the form breaks, as the FormBuilder object I used in the form_for is no longer available. Is there any way for me to use my custom FormBuilder object inside a partial used for an AJAX callback?
Use fields_for inside your partial. It performs a similar task but without wrapping the form tags. See the API docs.
how about this?
#template.with_output_buffer do
#template.form_for #model_object do |f|
f.fields_for :some_nested_attributes do |ff|
render :partial => 'nested_attributes', :object => #model_object, :locals => {:form => ff}
end
end
end
this would be especially useful is you need to use the nested fields_for in the partial
You could instantiate a new instance of your form builder in the controller, though it feels sort of lousy to me:
# in the controller
render :partial => {
:f => MyFormBuilder.new(:staff_member, #staff_member, template),
:skill_groups => #skill_groups,
:staff_member => #staff_member
}
Alternatively, you could move more of the update logic to be client side which wouldn't require you to worry about rendering anything at all. You could just update the values via JS. Not sure if that works for your project though.
Maybe I'm a little late in the game here, and maybe I don't understand the question properly, but in ApplicationHelper.rb I think you can just add the line:
ActionView::Base.default_form_builder = MyFormBuilder
You can submit within your ajax call the content of f.object_name (it's also works with partials) and use it to render tags defined in http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html passing it as the first argument.

Resources