How to ajax reload partial from partials - ruby-on-rails

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.

Related

Ajax form within a form with no layout

So I'm new to rails and having a little bit of trouble my situation is that I have a products model that has some Images attached to it. I would like on my products page to have a button to create a new image via ajax.
Inside my products _form view i have:
<%= simple_form_for(#product) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.input :name %>
<%= f.input :description, :input_html => {:class => "wysihtml5 span6", :style => "height:400px;"} %>
<%= f.association :images, label_method: :name, value_method: :id %>
<h4>Upload new file</h4>
</div>
<%= link_to 'Add Image', '/images/new', :remote => true, :"data-replace" => "#image-form" %>
<div id="image-form">
</div>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
And it does successfully Load the page via ajax.
But I would like to load it without the layout. Is this possible?
Do I need to create a new action that renders the partial form and has no layout??
So I'm assuming you have it requesting and returning html and not js? There are a couple of thoughts I have on this:
If you only ever need for this request to return from an AJAX request, then you could simply tell the controller to always render layout: false
If you would like to allow the controller to return a full page on occasion, you can either accept an argument in the get request and change the output accordingly, or you can tell the controller to:
respond_to do |format|
format.html # will render default with no block passed
format.js { render layout: false }
end
This should just work with the code in your form right now, because the :remote => true tells the controller to return js if possible, but accessing images/new from your browser will request html.
(Requesting js does not mean that you actually have to return js, as I don't in this case; it's up to you to take the proper action with what is returned. Some might consider it bad form, though, to return something other than what is technically requested.)
You can use the following in your action. Let your action is new.
def new
#your code goes here
render :layout => false
end

Rails 3 - Not able to pass local variable to a partial resursively

++ edited to change the question to call partial recursively ++
I am writing this question after a lot of days of frustration about this simple piece of code which isn't working. I have read numerous issues with passing partials on stackoverflow and tried all the solutions I could but it doesn't work.
Calling the partial works for the first loop but from second onwards it bombs with an error
"undefined local variable or method `count' for #<#:0x2f12670>"
Notice here that the error contains a class within a class, so something fishy is happening when the partial is being called recursively.
I have a partial called _form.html.erb which calls another partial _rule_action_fields.html.erb. I need to pass a variable from the form partial to the rule_action_field partial and here is the code:
_form.html.erb: (Please note that the builder object here is being passed from code I have not pasted)
<% #folder_count = 1 %>
<% #rule.rule_actions.each do |ruleaction| %>
<%= f.fields_for :rule_actions, ruleaction do |builder| %>
<%= render( :partial => 'rule_action_fields', :locals => { :f => builder, :count => #folder_count } ) %>
<% end %>
<% #folder_count = #folder_count + 1 %>
<% end %>
_rule_action_fields.html.erb
<p id="folder_fields">
<%= count %>
<label class="form_label">Move the files to </label>
<%= f.select(:folder_id, current_user.folders.collect { |p| [p.name,p.id] }, {:include_blank => true } ) %>
</p>
Unfortunately I cant call this partial with a collection of rule_actions because of some logic which is partially in the calling code and partially in the rule_action_fields partial.
Other syntax I have tried:
<%= render :partial => 'rule_action_fields',:f => builder, :locals => { :count => folder_count } %>
<%= render :partial => 'rule_action_fields', locals => { :count => folder_count } %>
<%= render 'rule_action_fields', :count => folder_count %>
Any ideas?

link_to_function does not react to click in rails 3.1.0 & simple_form

Here is the code in rfqs/_form_new.html.erb to add a selection box for standard.
<%= simple_form_for #rfq do |f| %>
<div id="std">
<%= render :partial => 'standards/standards', :collection => #rfq.standards, :locals => { :f => f } %>
</div>
<%= link_to_function "Add Std", nil do |page| %>
page.insert_html :bottom, :std, :partial => 'standards/standards'
<% end %>
<% end %>
The html source code has # after href and cause no reaction for clicking the link.
Add Std
The _standards.html.erb partial looks like:
<%= f.association :standards, :collection => Standard.active_std.all(:order => 'name'), :label_method => :name, :value_method => :id %>
Any thoughts about missing link after href? Thanks.
I don't believe 3.1's link_to_function works the same way as older versions; I don't see anything in the source that utilizes a block.
This seems in keeping with using unobtrusive JavaScript. The Rails pulls related to accepting a block seem more related to link text rather than injecting JS.

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

Ajax form not showing up with ruby on rails form_remote_tag

This is all new territory for me, but I am working through a Rails book that was written before start_form_tag was deprecated and I am running into problems with the books example code using remote_form_tag. I have the other forms working but can't get this one up and running. Here's the code:
<h1>Categories</h1>
<ul id="category_list">
<%= render :partial => 'category', :collection => #categories %>
</ul>
<br/>
<p id="add_link"><%= link_to_function("Add a category", "Element.remove('add_link');
Element.show('add_category')") %></p>
<div id="add_category" style="display:none;">
<%= form_remote_tag(:url => {:action => 'new'},
:update => "category_list",
:position => :bottom,
:html => {:id => 'category_form'}) %>
Name: <%= text_field "category", "name" %>
<%= submit_tag 'Add' %>
<%= end_form_tag %>
</div>
This is exactly how it appears in the book, and doesn't compile. I've tried changing to match the form_tag blocks but the form enclosed by the "add_category" div never shows up.
Thanks!
UPDATE: Just found that it doesn't appear that the prototype script is not getting loaded.
both the remove and show methods on Element are showing up as undefined in Firebug. I am not sure why it is not showing up though.
I left out the = sign when including the javascript libraries in standard.html
Line should be this:
<%= javascript_include_tag :defaults %>
Vote to close this question?

Resources