Rails: Rendering a partial to the right div? - ruby-on-rails

I have a loop in my view that renders many partials: each partial is a simple toggle to perform a save/unsave operation on a record. Each partial toggles the boolean field using javascript then updates itself, and the controller finishes by saying:
$ controller
render :partial => "save_unsave_buttons", :locals => {:matching => #matching}, :layout => false
# view
<div id=<%= "save#{match.id}" -%>>
<%= render :partial => "save_unsave_buttons", :locals => {:matching => match} %>
</div>
When the controller renders the save_unsave_buttons partial, it isn't rendering to the right div; it just updates the partial for the first record it finds. How can I ask the controller to render the save_unsave_buttons partial to the div with the right id?
Thanks!

Looking at only the little info that you provided for your problem i guess the culprit in the code is the div tag.
The div tag should be within the partial.
EDIT: What your code does is it creates a single div with id save(the first match.id) and renders the partial within it. If I understood you correctly you need a div for each match. For that the div itself should be within the partial.
So pass the match.id to the partial in a collection. Your view should be:
# view
<%= render :partial => "save_unsave_buttons", :locals => {:matching => match, :collection => {#match_id => match.id} } %>
and in your partial _sav_unsave_buttons.html.erb:
<% content_tag :div, id => "save#{#match_id}" do %>
#YOUR CODE GOES HERE!
<% end -%>

Related

How to ajax reload partial from partials

I have an app in which I different networks have messages.
In my network show view I display all the messages in with a partial "overview" which shows a link to the message content. The content of the messages is loaded with ajax inside #detailed div:
<div class="span2" id="sidebar">
<%= render :partial => 'shared/sidebar' %>
</div>
<div class="span4" id="overview">
<%= render :partial => 'overview' %>
</div>
<div class="span6" id="detailed">
<%= render :partial => 'nothing' %>
</div>
_overview.html.erb, in which i :
<h2>Messages</h2>
UNREAD:
<%= render :partial => 'items', :collection => #task.find_all{|item| item.unread == true }.sort_by(&:created_at).reverse, :as => :item %>
READ:
<%= render :partial => 'items', :collection => #task.find_all{|item| item.unread == false }.sort_by(&:created_at).reverse, :as => :item %>
_items.html.erb:
<%= div_for item do %>
<%= link_to(network_message_path(#network, item), :remote => true, :class => ["message item", ("unread" if item.unread == true)]) do %>...<% end %>
<% end %>
The message is contains a partial again with the full content, comments etc.. Code by which the message is loaded:
$("#detailed").hide().html("<%= j(render('show_message', :message => #message)) %>").fadeIn('fast');
If someone opens the detailed view, I update the unread status of the message to false. I would like to reload the overview partial, if someone loads the message into the details. One solution would be to just use jQuery to move things around, but the system will become more complicated with more filtering options. Therefor, reloading the overview partial would be a simpler solution. But what would be the fastes way to do so? As I need to reload the show method in the networks controller, alongside loading the show method in the message controller.
What would be the best solution, besides jQuery?
Created a module to load the overview partial with ajax.

Render a partial via page.replace_html and using a layout?

here is what I'm trying to do:
page.replace_html('manage_categories_list', :partial => "/categories/categories", :layout => :modal)
But, I get this error on the above code:
NoMethodError (undefined method `include?' for :modal:Symbol):
What I want to be able to do, is have template HTML for a modal dialog window. And set the header (h2) and the body (div) of that modal with any partial. =\
modal.html.erb:
<div class="fixed_modal">
<div class="modal_header">
<%= yield :header %>
</div>
<div class="modal_body">
<%= yield %>
</div>
</div>
The partial I'm trying to render:
<% content_for :header do %>
Manage Categories
<% end %>
.... rest doesn't matter as it just goes into the yield
I'm using rail 2.3.14
Most likely, you need to change it to ..., :layout => 'modal':
page.replace_html('manage_categories_list', :partial => "/categories/categories", :layout => :modal)
The #replace_html method accepts options like #render. For the :layout option, you either need to pass true, or a string layout file name (See the docs for #render).
The layout argument will be the 'xyz' part of the filename in app/views/layouts/xyz.html.erb.
The argument to :layout can't be a symbol try
:layout=>'modal'
This assumes you have a layout in app/views/layouts/modal.html.erb

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>

RoR: Replace_html with partial and collection not functioning

I am trying to create a tabbed interface using the prototype helper method "replace_html." I have three different partials I am working with. The first one is the 'main tab' and it is loaded automatically like so:
<div id = "grid">
<% things_today = things.find_things_today %>
<%= render :partial => "/todaything", :collection => things_today, :as =>:thing %>
</div>
...which works fine. Similarly, I have a _tomorrowthing partial which would replace the content in the 'grid' div like so:
<%things_tomorrow = things.find_things_tomorrow%>
<%= link_to_function('Tomorrow',nil, :id=>'tab') do |page|
page.replace_html 'grid' , :partial => '/tomorrowthing',:collection => things_tomorrow, :as => :thing
end %>
If I click on this tab nothing happens at all. Using firebug, the only errors I find are a missing ) after argument list which is contained in the Element.update block where the link_to_function is called. What am I doing wrong?
Hey Jack i try to reproduce the same but i can't i never used link_to_function before but
Following code may help to achieve the same you want
<%= link_to_remote "Today Thing", :url=>{:action=>"things", :id=>'today'}%>
<%= link_to_remote "Tomorrow Thing", :url=>{:action=>"things", :id=>'tomorrow'}%>
<div id = "grid">
<% #things = things.find_things_today %>
<%= render :partial => "/todaything", :collection => #things %>
</div>
in controller
def things
#things= (params[:id]=="today")? things.find_things_today : things.find_things_tomorrow
render :update do |page|
page.replace_html 'grid', :partial => (params[:id]=="today")? "/todaything" : '/tomorrowthing' , :objects=> #things
end

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