I currently am able to render a partial in JS like so:
$("#cmtList_<%= #commentable.id %>").prepend("<%=escape_javascript(render :partial =>"comments/comment", :locals => {:comment => #comment})%>");
The above works swell for rendering/returning one new comment.
Now I want to reuse that same partial(to stay DRY), but be able to pass an array of comments as #Comments, and have Rails loop through all the #comments records (1 or more) using the same partial, I tried this but it errors:
$("#cmtList_<%= #commentable.id %>").prepend("<%=escape_javascript(render :partial => "comments/comment", :collection => {:comment => #comments})%>");
Any ideas?
Thanks
Try this inside of escape_javascript:
render :partial => "comments/comment", :collection => #comments
Docs say that this should work too (too much magic for me):
render #comments
Check "3.4.5 Rendering Collections" from here:
http://guides.rubyonrails.org/layouts_and_rendering.html
Try:
$("#cmtList_<%= #commentable.id %>").prepend("<%=escape_javascript(render :partial => "comments/comment", :collection => #comments)%>");
which will look for a local variable "comment".
For more info: http://guides.rubyonrails.org/layouts_and_rendering.html
Related
Right now I have something like this:
<%= render :partial => "widgets/type1.html.erb", :locals => {:widget => #wall} %>
I have 50 different types
widgets/type1.html.erb
widgets/type2.html.erb
widgets/type3.html.erb
widgets/type4.html.erb
....
In the widget object, there is a widget.type which has the type2 etc... Is there a way I can make the partial dynamic to avoid all the if statements?
Thanks
Since you can tell rails to render :partial=>string, you can do string replacement inside that string. So, if #widget is the variable name:
<%= render :partial => "widgets/#{#widget.type}", :locals => {:widget => #wall} %>
In my new.html.erb page, i use the following line to render a partial and it works fine.
<%= render :partial => "submissions/player_form", :locals => { :submission => #submission } %>
Now i want to render exactly the same partial via RJS
<p>Player Type: <%= f.select(:PLAYER_TYPE, $playersList, {:prompt => 'Select the Player Type'} %></p>
<%= observe_field("submission_PLAYER_TYPE", :frequency => 1,
:url => { :controller => 'submissions',
:action => :display_player_form },
:with => "'player='+value") %>
display_player_form.rjs:
page.replace_html 'observed_assay_form', :partial => 'submissions/player_form', :locals => {:submission => #submission }
Nothing is displayed!!
Am i missing something??
Thanks for helping me out with this :)
I finally figured it out. So here are my findings:
In the partial, include the form_for tag, just like in the original form--
<% form_for #object do |f| %>
In the action used when observing the field, in my case, 'display_player_form', create a new instance of the object(see below)
#object = Object.new
In your rjs file, enter the following:
page['id of div'].replace_html :partial => 'your_partial_name'
There you go...
Hope this helps
I would rename display_player_form.rjs to display_player_form.js.erb and have its contents look like this:
$("#observed_essay_form").html('<%=
escape_javascript(
render :partial => 'submissions/player_form', :locals => {:submission => #submission }
)
-%>');
$("img[src$='spinner.gif']:visible").hide(); // optional - hide any visible spinner.gif images
I use jQuery, not Prototype, by the way.
I am calling:
render #users, :layout => "homepage"
because I want to wrap the default partial for users (views/_user.html.erb) with a custom layout just for the homepage (views/users/_homepage.html.erb).
but, when I do this, I get the NoMethodError on the user.name method.
For some reason it seems like the user variable is not getting initialized properly inside the user partial.
It turns out after some test, the homepage partial is not even getting called, it is going straight to the user partial ....
This is not the solution I wanted, I believe there may actually be a way to make this work using just a call to render, but this is what gave me the correct output:
#users.each do |user|
render :partial => "users/user",
:layout => "users/homepage",
:locals => { :user => user }
end
Or is it that the :layout option only works when rendering a single resource and not a collection?
As of Sept. 2019, in Rails 6, this is how we are doing this:
<%= render partial: 'homepage_user_list_entry', collection: #users %>
With alias:
<%= render partial: 'homepage_user_list_entry', collection: #banned_users, as: :user %>
Hope this helps future searchers, and also me in the future.
Try adding :as => :user to render a partial from a view:
<%= render :collection => #users, :as => :user, :partial => 'users/user_short_form', %>
You should do something like
<%= render 'homepage', :collection => #users, :layout => 'homepage' %>
not sure about the :layout option, but you have to pass #users thro :collection
hope it helps!
Not sure if this is a newer addition to Rails, but in Rails 4.2.1 I can pass my collection to the partial argument of render:
render partial: #users, layout: "homepage"
I'm trying to learn Rails better by looking at example applications, and while looking at this line of the source of railscasts.com, I noticed it does this:
<div class="episodes">
<%= render #episodes %>
</div>
What exactly is going on here? Why isn't this documented on the render function? Or is it?
This is a handy shortcut for doing
<%= render :partial => "episode", :collection => #episodes %>
which is another way of doing
<% for episode in #episodes do %>
<%= render :partial => "episode", :locals => { :episode => episode }
<% end %>
which is pretty obvious in what it does :)
Hope that makes sense :)
btw it's really surprising I couldn't find the docs for this too.
This is shorthand for
render :partial => "episode", :collection => #episodes
The form above is documented in the Rails API docs under render (ActionController::Base). The shorthand form is not documented as far as I can see except in the Rails Guides.
This is a new shortcut:
<%= render #episodes %>
# equivalent to
<%= render :partial => 'episode', :collection => #episodes %>
You can also do shortcuts with single items
<%= render 'comment', comment => #comment %>
# equivalent to
<%= render :partial => 'comment', :locals => {:comment => #comment} %>
I render a partial like so:
<%= render :partial => 'widgets/some_partial, :locals => {:foo => 'bar'} %>
So inside of _some_partial.html.erb I render two more partials like so:
<% #foo.nil? #=> false %>
<%= render :partial => 'widgets/another_partial', :locals => {:foo => foo} %>
`<%= render :partial => 'widgets/another_partial_again', :locals => {:foo => foo} %>`
The foo local variable renders fine in some_partial.html.erb and even in another_partial_again.html.erb. However, the foo variable is inaccessible in another_partial.html.erb even though I explicitly passed it in the render call.
What is happening here?
Thanks for the help.
I had the undefined local variable or method error come up for me too when I was rendering a partial with :locals defined.
However, I had a different issue causing my problem, so I thought I would share my solution in case it helps anyone else. (This page was the first result when I googled this error after all)
Basically just make sure you use :partial => 'path/to/partial' in your call to render.
I.e.
<%= render :partial => 'widgets/some_partial', :locals => {:foo => 'bar'} %>
NOT like I was doing:
<%= render 'widgets/some_partial', :locals => {:foo => 'bar'} %>
Easy for a rails/ruby newbie like me to miss.
Solved. Turns out I was also rendering the same partial from the controller without sending the proper local variables. Thanks anyways!!!
Bumped into this very old question cause I faced the same issue.
Turned out that with Rails 4+ if you are not using collections or layout the correct way is:
# Instead of <%= render partial: "account", locals: { account: #buyer } %>
<%= render "account", account: #buyer %>
As documented here.