How to modify the default RoR behavior to AJAX CRUD? - ruby-on-rails

I want a table to do CRUD, but I want it in an Ajax way, how can I modify the default RoR behavior, any suggestions on that?
I tried comment these code in destroy method, but it don't work, it saw missing the destroy.erb:
respond_to do |format|
format.html { redirect_to(categories_url) }
format.xml { head :ok }
end

It will default to HTML if you don't give (and accept) a different response. Check out this railscast to see how to set up an AJAX request and response with RJS (there's another on how to do it with jQuery).
Basically you'll need a format.js line. You can also make a view of the form .js.erb where you write JavaScript to run on the server response.
As a side note though, unless you have a very specific website, I'd suggest not doing only AJAX. It's not very accessible for users without JavaScript, like blind users with a screenreader.

Related

Rails redirect with html respond with js

I know this question has been asked in different ways before, but in my case, I reviewed many answers and nothing seems to work. My case is simple:
I am making an api call to https://www.facebook.com/v4.0/dialog/oauth?... with a redirect to /auth which is a GET. It returns a code with which I am retrieving the access_token.
I added a auth.js.erb that should open a modal.
Very basic.
After storing the access_token, I want to redirect to the user profile and open up a modal with JS to show the user some more info.
For some reason my JS in respond_to never gets executed.
Here's my simple code:
def auth
response = RestClient.post(
"https://graph.facebook.com/v4.0/oauth/access_token",
{
grant_type: "authorization_code",
code: authorization_code,
...
}
)
...
respond_to do |format|
format.html {redirect_to edit_user_registration_path} # This works
format.js # Never gets executed
end
end
Any ideas?
You might need to specify the format in url. Tried this?
yoursite.com/auth.js
or
yoursite.com/auth?format=js
It might depend on your routes, and if you're accessing via browser url or ajax, etc.

How do I display a d3.js circle pack graph with RoR?

I am trying to set up a graph like this http://bl.ocks.org/4063269#index.html with d3.js:
I need to do this by putting methods in a controller and the js in a .html.haml file. I have made the controllers, but have absolutely no idea how to write the methods.
The methods need to take values from a sqlite3 database and convert it into JSON for the d3.js to use. Could someone get me started? I have no idea what to do right now...
Unfortunately the question you are making is too open, with more details you will get more help ;)
So the javascript library is going to make ajax call to your application, and your application should respond with json?
In that case, you can just do the routing on config/routes.rb, and then you just write a method like this:
class MyController < ApplicationController
def values_for_js
my_data = MyModel.calculate_data
respond_to do |format|
format.json { render json: my_data.to_json }
end
end
end
And your js should request something like http://mywebsite.com/values_for_js.json (or it can say in the ajax request that a json format is expected). If you are not able to make the json request, you can just use format.js instead of format.json

Why do controllers need multiple format renderings?

Please explain why do we need this code in a controller? What is the significance of this block of code?
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
It allows you to format output differently depending on the format the user/caller requests. If you were to access http://yourhost/controller/index.html, the controller would respond with the ERB template index.html.erb (or HAML or whatever). If you were to access http://yourhost/controller/index.json, it would respond with the JSON template index.json.erb.
This allows you to have a single controller action that can prepare data and then select the view for rendering based on the requested format.
Defines mime types that are rendered by default when invoking respond_with.
So basically, this means that your controller action can be hit in different formats(html, json in your case), and still provide data back to whatever is calling it. This is helpful for API development, and many other things.
For example: You want to get a json list of all your users to do something with javascript. You'd call /users.json and this would go to your user_controller#index action and know to render a json object of all your users.
The above code is scaffold generated, and provides a way to render *.html and *.json views for your controller, making it easy to have access to data for implementing an API or normal views for your web application.
You can also create XML output:
format.xml { render xml: #users }
and other formats like PDF, or DOC, depending on the gems you are using.
See the Rails Guide Action Controller Overview for more information.

Rails 3: HTTP 406 and blank page on trying to view json in browser

I am a little new with Rails and ajax, and I'm encountering the following frustration whilst trying to debug some fairly simple ajax requests in my controllers.
I have the following code:
respond_to do |format|
#format.html { render :json => db_clean_response.to_json }
format.json { render :json => db_clean_response.to_json }
end
When I try to debug this by manually entering the URL and parameters, the controller correctly prepares the response, but Rails responds with HTTP/406 Not Acceptable unless I uncomment the format.html command. (Note that the format.html stuff is just for trying to fix this.)
I don't want this function responding with html, as its only for ajax stuff. Besides, Rails responds with the json uglyprinted onto empty html, and I'd like to use a plugin like JSONView to prettify it.
What's going on here? I feel like the desired result is very simple, but something, somewhere is messing it up. When I try to debug ajax I want my browser to pull up the damn json without being lame :-(
It turns out that adding format=json to the URL parameters does what I want: forces Rails to return json with all the right headers

Using respond_to for graceful degradation with ajax in RoR 2.x

I was going through the AWDR book on web development with ruby on rails and one of the issues with the old code was it didn't use respond_to to make sure the view used would be the javascript view. Now in some updated examples I've seen people mention they later, when implementing graceful degradation, use request.xhr? to tell if the user has javascript enabled, and if not they redirect the user.
I was wondering if you could use respond_to to get the same behaviour and if so, if this is considered good form or not and why?
So what I'm thinking of doing is something like:
def function
respond_to do |format|
format.js do
basic_stuff
end
format.html do
basic_stuff
user_redirect
end
end
end
It does seem to sorta violate the DRY principle, and I'm probably missing something about how the user and the server are interacting here. To be honest the API documentation did not make it entirely clear to me.
Well you can refactor like this:
def function
basic_stuff # executed regardless of the mime types accepted
respond_to do |format|
format.html do
user_redirect
end
end
# will fall back rendering the default view - which you should ensure will be js
end
request.xhr? looks at the request‘s X-Requested-With header (to see whether it contains "XMLHttpRequest"). respond_to looks at the mime types accepted.
You can use either to implement some sort of graceful degredation.
BUT You won't be able to use xhr? for graceful degredation unless your ajax calls are setting that header (Prototype does this automatically).
Moreover, respond_to gives more flexibility i.e. sending xml, json, js, whatever it might be from the same block.
So I'd recommend respond_to here.

Resources