Replace render :update for rails 3.1 - javascript

It seems that the method render :update is not supported anymore in Rails 3.1, now I'm checking what the best way is to change this code :
def create
#address = #current_user.addresses.build(params[:address])
#address.save!
respond_to do |accepts|
accepts.html {
flash[:notice] = t(:"notices.address.created")
redirect_to :back
}
accepts.js {
flash[:notice] = t(:"notices.address.created")
render :update do |page|
page.redirect_to(:back)
end
}
end
end
I just need to do a redirect :back when the action is succesful, but for the moment he just try to render the update partial.

render :js => "window.location = #{escape_javascript(request.env['HTTP_REFERER'])}"

Related

Display a notice in rails after an ajax call

Hi I have a status controller where I render js template when a user deletes a status. This is the controller code:
def destroy
#status = Status.where(id: params[:id]).first
if #status.present?
make_subject(#status, Constants::Subject::BREAK)
respond_to do |format|
format.html { redirect_to root_path, notice: 'deleted' }
format.json { head :no_content }
format.js { render layout: false }
end
end
end
In my destroy.js.erb, I have:
$("#clear_status_<%= #status.id %>").fadeOut();
I have a partial which renders flash messages
<div id = "notice_wrapper">
<% if alert %>
<div class = "alerty">
<p> <%= alert %> </p>
</div>
<% elsif notice %>
<div class = "notice">
<p> <%= notice %> </p>
</div>
<% end %>
</div>
Once the status gets deleted successfully I want a notice which informs the user that the status is deleted successfully I want to display that message by rendering this partial.
How can I achieve this?
You can do it with JQuery. If put your own element instead of 'nav' where u want to show notice.
$('nav').after("<div class='alert alert-success'> Successfully Destroyed </div>");
$("#clear_status_<%= #status.id %>").fadeOut.fadeOut(250, function(){
$(this).remove();
});
Also Update your destroy method.
def destroy
#status = Status.find_by(id: params[:id])
if #status.present?
make_subject(#status, Constants::Subject::BREAK)
respond_to do |format|
#format.html { redirect_to root_path, notice: 'deleted' } # yr calling it async to destroy. format.html is needless.
format.json { head :no_content }
format.js
end
end
end
You will need a way to tell in your response if the delete was successful. And if it was, then show your notice.
For this purpose, I'll create a #deleted variable which will be boolean, and will be set to true if delete is successful, otherwise, false.
Following is the controller code:
def destroy
#status = Status.find(params[:id])
#deleted = false
if #status.destroy
#deleted = true
end
respond_to do |format|
format.html
format.json
format.js
end
end
and the destroy.js.erb file, you will have something of this sort:
<% if #deleted %>
$(".notice").html(<%= render 'notice_partial' %>)
<%= escape_javascript(alert("deleted successfully")) %>
<% end %>
$("#clear_status_<%= #status.id %>").fadeOut();

Rails AJAX Destroy - Re-render index issues

I have an issue with a Rails AJAX app which is confusing me, even though it seems very simple! I am dealing with class Order in a simple point of sale rails app. The request is being made as the order will be deleted on page refresh (but I am getting no refresh of #orders) as I thought I am specifying in destroy.js.erb.
orders/index.html
<div id="orders">
<%= render 'orders/index' %>
</div>
<%= link_to 'New Order', new_order_path, remote: true %>
<div id="order-form" style="display:none;"></div>
orders/_index.html
<% if #orders.any? %>
<% #orders.each do |order| %>
<%= link_to "Show #{order.id}", order_path(order), class: "something" %>
<%= link_to "Delete #{order.id}", order_path(order), method: :delete, class: "something" %>
<%= link_to "Delete order with ajax", order_path(order), remote: true, method: :delete %>
<% end %>
<% else %>
<p>No orders yet</p>
<% end %>
destroy.js.erb
//When deleting order on order index - render orders again.
$('#orders').html("<%= j (render 'orders/index') %>");
and the relevant actions from orders_controller.rb
class OrdersController < ApplicationController
respond_to :html, :js
def index
#orders = Order.paginate(page: params[:page])
if params[:search]
#orders = Order.search(params[:search]).order("created_at DESC")
else
#orders = Order.all.order('created_at DESC')
end
end
def destroy
#order = Order.find(params[:id])
if #order.destroy
flash[:notices] = ["Order was successfully deleted"]
redirect_to orders_path
else
flash[:notices] = ["Order could not be deleted"]
render order_path(#order)
end
end
I suspect the issue is in my orders_controller destroy or index action, but I am a little unclear on a number of the ways of working with AJAX in Rails.
Link to repo - https://github.com/benhawker/point-of-sale-rails
Might be because after your destroy you're redirecting to the index path
I am getting no refresh of #orders
Your JS is likely not firing, you'll need the following:
def destroy
#order = Order.find params[:id]
respond_to do |format|
if #order.destroy
format.js
format.html { redirect_to orders_path, notice: "Order was successfully deleted" }
else
format.js
format.html { render order_path(#order), notice: "Order could not be deleted" }
end
end
end
This will fire app/views/orders/destroy.js.erb, which seems okay in your OP.
Try Updating your destroy action to
#order = Order.find(params[:id])
#order.destroy
#orders=Order.all
remove all the redirects
will work.

Ajax call with dropdown list

I have trying to follow this question but I am stuck.
In my controller I have:
def index
if params[:sort] == 'stars'
#projects = [Project.first]
else
#projects = Project.all
end
respond_to do |format|
format.html
format.js { render 'populate_projects', :formats => [:js] }
end
end
in routes:
get '/inspire/:sort' => 'projects#index'
in view:
= collection_select :project, :id, Project.all, :id, :name, {}, {:onchange => '$.get("/inspire/stars")'}
%div#normal
= render 'projects_list'
%div#stars{ style: 'display: none' }
my _projects_list.html.haml has:
%div
- #projects.each do |project|
%div
%p
#more code...
and finally in populate_projects.js.haml:
:plain
$("#stars").html("#{escape_javascript render(partial: 'projects/projects_list')}");
$("#normal").hide();
$("#stars").show();
Probably the program doesn't make sense as I am testing if ajax call is working. However, what should happen is when I change the state of dropdown an ajax call must be made which renders 'propulate.js.haml' and list of projects must change from all to just first, but is not. In my terminal I can see that call is being made but 'populate.js.haml' is never rendered. Can someone please help!
Make sure your Ajax call requests the JS format:
= collection_select :project, :id, Project.all, :id, :name, {}, {:onchange => '$.get("/inspire/stars.js")'}
If you want to make an ajax call you need to include:
"data-remote" => true
See here for more information

Hot to respond js in rails if action has no form?

I trying make notification system and have problem
I need publish notification to the view with this:
track_activity.js.erb
<% publish_to "/notifications/new" do %>
$("#activity").append("<%= j render(#activity) %>");
<% end %>
and my application_controller with action track activity
def track_activity(user, trackable, action = params[:action], author = current_user)
#activity = Activity.create! user: user, trackable: trackable, action: action, author: author
end
and i using this methos every time when oher user create something like this
comment_controller.rb
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.build(comment_params)
#comment.user = current_user
if #comment.save
track_activity #post.user, #comment
redirect_to post_path #post
else
render 'new'
end
end
How to attach track_activity.js.erb every time when rails call track_activity action?
respond_to in track_activity action doesn't help

Getting model in Rails through ajax from another controller

Solved this:
I have a following situation:
I have Graph and Node model.
While on graph/show.html.erb I make an ajax post with an id of a Node:
var val = document.getElementById('nodeName').value;
if(val){
$.post("/nodes/this_node/", {id:val});
console.log(val);
}
How do I get that Node to render on the same page in, for example, ?
EDIT::
I have a custom POST action in node_controller :
def this_node
#node = Node.find(idd)
respond_to do |format|
format.js
end
end
I had to add app/views/nodes/this_node.js.erb with:
$("#node_div").html('<%= escape_javascript(render :partial => "node_content", :locals => { :node => #node}) %>');
and a partial in views/nodes/_node_content.html.erb
<%= #node.content %>
All works.
EDIT:
Actually fixed it (above code works)

Categories