Rails - render partial collection as not working - ruby-on-rails

I am trying to render a partial of a collection using a name different to that of the model:
<%= render "current_campaigns", collection: #current_campaigns, as: :current_campaign %>
The model is called Campaign but this is a subset of campaigns as defined in the controller action:
def index
#current_campaigns = Campaign.where(status: :approved)
end
In the partial (which lives in the application directory not the campaigns directory):
<%= current_campaign.question %>
The resulting error:
undefined local variable or method `current_campaign' for #<#<Class:0x007fad3d5e5500>:0x007fad451976a8>
I was under the impression that as would make this work but apparently not. Any thoughts?

Try the following
<%= render partial: 'path-relative-to-views/current_campaigns', collection: #current_campaigns, as: :current_campaign %>
And the partial should name _current_campaign.html.erb for convention.

The most simple way to pass attributes to the partial using a custom name is:
<%= render "current_campaigns", custom_name: #current_campaigns %>
doing this you will be able to use custom_name as a defined variable on your partial.

Try to rename campaigns/current_campaigns.html.erb template to campaigns/campaign.html.erb and render like that from campaigns/:
<%= render #current_campaigns %>
Otherwise you can specify name of directory where the partial is:
<%= render "other_folder_name/campaign", collection: #current_campaigns %>

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

rails render collection and locals

I am trying to render a partial from within a js.erb file which has both a collection AND a local variable passed to it. Everything works fine except I cannot access the local variable in the view. I have tried the following:
<%= escape_javascript(render(partial: 'tasks/task', collection: #task_group.tasks_belonging_to, as: :task, locals: {testvble: "bob"})) %>
<%= escape_javascript(render(partial: 'tasks/task', collection: #task_group.tasks_belonging_to, locals: {testvble: "bob"})) %>
<%= escape_javascript(render(partial: #task_group.tasks_belonging_to, locals: {testvble: "bob"})) %>
<%= escape_javascript(render(#task_group.tasks_belonging_to, testvble: "bob")) %>
Each of the above works until I try to access the local variable in the view. I get the error:
undefined local variable or method `testvble'
Just for completeness, each of the above is inside the following statement in the js.erb file (at the xxxx position):
$("#task-group-<%=#task_group.id%>").append("xxxx").hide().show('slow')
This is the code in the partial causing the issue:
<p>should be bob:<%= testvble %></p>
The bottom syntax is actually correct as suggested by J Plato. My issue was that the partial recursively called itself if there were child tasks to display. When rendering the child task(s) the local variable was not being passed on.

Ruby on Rails pass reference to yield (template)

To make long story short: each of my tabs has its own form, so I decided to make a single layout and just to have a forms themselves as a variable content for a layout.
But I need to have form_for to be in a layout, rather then having it in each of the forms, because I have some other common form elements in the layout.
So how can I pass the form builder's reference f to the template ?
Layout code:
<% content_for(:content) do %>
<%= form_for current_form do |f| %>
<%= yield %>
<%= f.submit "Submit" %>
<% end %>
<% end %>
Is it possible ?
P.S Found this one: DRYing up a helper: wrap form_for and access local form variable (#rubish's answer), but <%= yield f %> doesn't seem to be working, f still remains undefined for the view.
Why don't you make a common template (not layout) for the tabs, and use a partial template for the content of each tab?
Then you can do something like:
<%= render :partial => #tab_name, :locals => {:form => f} %>
You can render a template in Rails that accepts a block by using the layout option of render
Let's say you have a form that you render a few times but you would like to customize your submit section each time. You can achieve this by rendering your form partial as layout and passing in a block. Your template or partial then serves as the surrounding layout of the block that you pass in. You can then yield back the form to the block and access the form in your block.
record/_form.haml
= form_for record do |form|
...
.form-actions
yield(form)
In order to make your template record/_form.haml accept a block when you render it, you can render your template as the layout for your block using the layout option of render:
record/edit.haml
= render layout: 'form', locals: { record: record, ... } do |form|
.form-actions--primary
= form.button :submit

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.

Rails partial collection empty?

I've been trying to figure this one out for about an hour now. I have a partial collection that is indeed populated with member model objects, and when I try to render it using collection:
<%= render :partial => 'list', :collection => #members %>
In my _list.html.erb the #member_id and #member_email attributes are empty in the rendered html:
<%= check_box_tag('selected_member[]', #member_id, false) %>
<%= #member_email %>
The view renders correctly and I get 3 member items, but with the id and email blank.
What am I doing wrong? If I try to access the partial member using "member_id" or "member.id" I get "undefined local variable or method `member'", so clearly that's not the correct notation.
Now I've seen a bunch of different examples, but the collection method is the way I need to go. I need the partial to only deal with each member as the parent view has support for grouping members in different ways. If I implement a loop in the partial for each member, I'll have to move the grouping view code into it as well, which interacts with input fields in the parent view. e.g. it would be a mess to maintain.
Thanks in advance.
When you do:
<%= render :partial => 'list', :collection => #members %>
Rails is expecting the local variable within the partial file to be named the same as the partial file itself, in this case list.
So instead of member, by default it would be list like this:
<%= check_box_tag('selected_member[]', list.id, false) %>
<%= list.email %>
If you want the local variable to instead be named member, you should either rename the partial file or use this syntax:
<%= render :partial => 'list', :collection => #members, :as => :member %>
More info here (Sec. 3.4.5): http://guides.rubyonrails.org/layouts_and_rendering.html

Resources