Render javascript for partial - Rails 3 - javascript

I'm still relatively new to rails and getting used to a few things. Just a quick question really, I have an AJAX form (with :remote => true etc...) which works and sends the data to the server without a pageload.
My question is around the javascript file that can be used after the post. My partial is called '_newsomething.html.erb' and once it's posted I want to execute a javascript file - do these files have to be named after actions in the controller to sync and work properly in rails? At the moment I have a javascript file called 'create.js.erb' with just an javascript alert in it, but it's not firing.
e.g. do I need to have a partial called '_create.html.erb' and then a javascript file called 'create.js.erb'? Is there anyway of telling Rails where to go for a javascript file from within the controller?
Thanks in advance!
EDIT: Controller code below (very basic and generated at the moment)
def create
#snip = Snip.new(params[:snip])
respond_to do |format|
if #snip.save
format.html { redirect_to(#snip, :notice => 'Snip was successfully created.') }
format.xml { render :xml => #snip, :status => :created, :location => #snip }
else
format.html { render :action => "new" }
format.xml { render :xml => #snip.errors, :status => :unprocessable_entity }
end
end
end

In your controller action you have to have:
respond_to do |format|
format.html { .... }
format.js
end
The format.js is the important part. If Rails discovers that you are POSTing through AJAX, then it will render _create.js.erb partial.

Related

ruby on rails rendering json in js.erb

In a create method in controller I have the following code:
respond_to do |format|
format.js { render json: { html: render_to_string(partial: '/users/photos/card', object: #photo, as: 'photo') } }
end
which works perfectly. Now I would like to move this in create.js.erb to have the possibility to execute additions js. However I do not figure out how to render the json above in create.js.erb.
Any idea how to accomplish this. Thanks in advance!
If I understood you right, what you are trying to achieve is to be able to access your JSON inside a js.erb view. In order to accomplish that, what I've done in the past is the following:
In your create.js.erb:
var yourJSON = JSON.parse("<%= raw(j render :partial => '/users/photos/card', object: #photo, as: 'photo') %>");
And in your controller:
respond_to do |format|
format.js # This will render the create.js.erb
format.json { render :partial => '/users/photos/card' } # Leave this one in case you also want to be able to respond to this format i.e: "http://localhost:3000/users/photos/card/id.json"
end
Hope this helps!
P.S: Let me know if you were trying to achieve something different.

backbone-rails: redirection to a controller action

I am using rails-backbone gem for the first time. I have to doubt in how to redirect in using backbone. Following the steps that i followed as provided at github's page and everything is working fine. but when i see my controller it contains the following line in create action
respond_to do |format|
if #login.save
format.html { redirect_to #login, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: #login }
which works great but I can see that for json object it renders show action and hence not possible to reload the page as it takes ID as attribute, I can use a static page for display is one solution but i need it to redirect it to show action rather than rendering it as done when the format is html. how can that be done?
This is my complete controller:
class LoginsController < ApplicationController
before_action :set_login, only: [:show]
def index
end
def new
#login = Login.new
end
def show
end
def create
#login = Login.new(login_params)
respond_to do |format|
if #login.save
format.html { redirect_to #login, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: #login }
else
format.html { render action: 'new' }
format.json { render json: #login.errors, status: :unprocessable_entity }
end
end
end
private
def set_login
#login = Login.find(params[:id])
end
def login_params
params.require(:login).permit(:email, :password)
end
end
There are many ways to do it.
1)In you controller instead of rendering json object, just create create.js.erb under views/logins then put js code to redirecting the page. It should be something like this,
create.js.erb:
window.location.href = <%= users_path(#user) %>
2) You can execute this window redirection js code in the backbone callback after the successfull creation of record. Please refer this backbone affcial website to know how to success callback.

Comprehensive ajax page for Rails

I have a Controller named ProductsController. In that controller are several actions such as socks and towels. I also have views of socks and towels. The view pages of socks and towels have the exact same form inside it. I'm going to use ajax in both files for the forms. Since the form is the exact same in both views, I see it pointless to create multiple ajax js files for the actions to call when ajax is called. How can I have both actions socks and towels call the same js.erb file instead of socks.js.erb and towels.js.erb respectively?
My controller and the action socks.
def socks
#socks = Socks.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #socks }
#ok, I get the line below is needed for ajax. Is there something I could add for
#the action not to call socks.js.erb and instead a different file, say ajax.js.erb
format.js
end
end
I think you're looking for render here. Using it you can render a template/view of another action. See the doc: Layouts and Rendering.
render 'products/show'
render :template => 'products/show'
render "/u/apps/warehouse_app/current/app/views/products/show"
as Sergio rightly said, you can do exactly that using the render command. Basically, in both socks and towels actions, you can render a common ajax.js.erb file when the respond_to is for JS.
I am adding this answer however because of your comment. It seems that your socks and towels methods as well as the forms and associated responses are very similar. If that is the case, why not use a single action for the same? Basically, you can use a URL parameter to specify the nature of product in the ProductsController actions.
So you would have something like:
def ProductsController
def index
if params[:nature] == "socks"
#socks = Sock.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #socks }
format.js
end
elsif params[:nature] == "towels"
#towels = Towel.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #towels }
format.js
end
end
end
end
Now you would need 2 view files index.html.erb and index.js.erb and there you can check the params value again and display the relevant collection.
This is not an ideal solution, but more a quick fix.
Failing that, you can also define a new action method in the ProductsController and have both forms point to that.

Rails 3.1 Ajax question

I have a scaffold called post which has a title and a description. On my layout I have a link to create a new post that has :remote => true. How would I make it when I click on that remote link to change the content of a div so that I can create a new post?
Let's suppose the action you will use is called new.
You should create a file called new.js.erb into views/posts that will be rendered when you post remotely your form. That file must include the javascript that places the new post into the div you want to fill. As an example, it could contain
# new.js.erb
$('div#container').html("<p><%= escape_javascript(#post.title) %></p>").append("<p><%= escape_javascript(#post.content) %></p>");
The javascript will be executed immediately after the ajax post is finished and the new post is created. Remember the following:
- You have to include jQuery
- You have to specify in posts_controller the ability to render .js format, something like
# posts_controller.erb
def create
#post = Post.new(params[:post])
respond_to do |format|
if #post.save
format.html { redirect_to(#post, :notice => 'Post created via non AJAX.') }
format.js # the actual ajax call
else
format.html { render :action => "new" }
end
end
end

Can I redirect_to a javascript request to another javascript action?

I have a comments controller with index and create actions among others. Both those actions respond to html and js format.
Whenever I got create request via ajax, I would like to add new comment and then redirect to index.js, so the comments on screen are updated without reloading the page.
This sort of thing works flawlesly in Chrome. But whenever I try this in Firefox or IE, it turns out, that the redirect from create.js lands in index.html...
Even when i force the redirect to be js:
redirect_to polymorphic_path([#commentable, :comments]), :format => 'js'
It land up in the format.html in Firefox and IE.
Any idea what might be happening here?
There are various issues with the way browsers handle 302 requests differently. Some lose request types, others lose request method (an example ticket: http://trac.tools.ietf.org/wg/httpbis/trac/ticket/160).
I would suggest that rather than redirecting to a new URL when using JS, you simply render the same action. So something like this:
class CommentsController < ApplicationController
def index
setup_for_index
respond_to :html, :js
end
def create
# Creation stuff...
respond_to do |format|
format.html {redirect_to :action => :index}
format.js do
setup_for_index
render :action => :index
end
end
end
private
def setup_for_index
#comments = ...
end
end

Categories

Resources