Partial not rendered via RJS - ruby-on-rails

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.

Related

ajax call with rails not updating content

i'm trying to get the content of a div to change when i click a link. i can see from the console that the partial and the js is being rendered, however, my html does not update
in index.html:
<div id="test">this is supposed to change</div>
...
<%= link_to 'planner', test_path(:budget => "b2", :size => "g2", :age => "a1", :activity => "l2", :tourist => "t2", :city => "boston"), :method => :get, :remote => true, :id => "linkto" %>
in my planner_controller:
respond_to do |format|
format.html
format.js {render :layout => false}
in update.js.erb:
$("#test").update(" <%= escape_javascript(render('load')) %>")
_load.html file:
<p> test </p>
could the error be that i'm just testing the code, and not actually doing anything with the results of the query?
You are updating an element with an id of test but you don't have that element anywhere on your page.
$("#test").update(" <%= escape_javascript(render :parial => "load") %>")
Try this
You can use replaceWith() in place of update()
$("#test").replaceWith(" <%= escape_javascript(render('load')) %>")

How to pass another flag variable in addition to collections to a partial?

How can I pass a is_featured = true to the following partial?
<%= render :partial => 'stores', :collection => #stores %>
I only need to pass the is_featured in one place (in all other places I call the partial as above.
You can use the locals option of render
<%= render :partial => 'stores', :collection => #stores, :locals => { :is_featured => is_featured } %>
In the partial you would access it as a method:
<%= is_featured %>

Trouble on loading multiple partial templates in a view

I am using Ruby on Rails 3.0.7 and I would like to understand how can I handle the following situation to avoid the istance variable #photo overriding on loading the view.
In my view file I have:
<div>
<%=
render :partial => 'user/photos',
:locals => {
:photo => #photo = 'test_photo1.jpg',
}
%>
</div>
<div>
<%=
render :partial => 'user/photos',
:locals => {
:photo => #photo = 'test_photo2.jpg',
}
%>
</div>
If I load the above view I have a strange behavior on outputting. That seams that the #photo class is overwritten (by the second rendering statement) when the page loads. Of course if I make only one rendering all works.
How can I solve the above problem in order to properly pass variables?
Note: I can not change the #photo name. That is, it must be the same for both partial templates.
I also tryed this version just deleting the #photo variable
<div>
<%=
render :partial => 'user/photos',
:locals => {
:photo => 'test_photo1.jpg',
}
%>
</div>
<div>
<%=
render :partial => 'user/photos',
:locals => {
:photo => 'test_photo2.jpg',
}
%>
</div>
but that still doesn't work.
<div>
<%=
render :partial => 'user/photos',
:locals => {:photo = 'test_photo2.jpg'}
%>
</div>
Try that, I dont know why you had :photo => #photo = 'test_photo2.jpg'
Also see the Rails guides on partials. They really let you have a good grasp of the basics.

What does "render #collection" do?

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} %>

Rails partial locals not persisting when sent to another partial as its own local

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.

Resources