Let's assume I have a Users controller with a returnjson action. Say I want to create an API so when the client calls on that method, it would return the user's data to the client. For example:
https://www.example.com/returnjson?username&password
A get request would be made by JavaScript with that URL and than the rails would than return the user info in a JSON format. How would I got about doing this?
Thanks in advance!
Oh i see, you only want to return the attributes requested in the parameters. Sorry.
First of all, i would just us the show action for this rather than making a new action. The purpose of show semantically is to return data for a single record, so it's the right action for this job.
Your parameters for the request aren't well structured - i would structure them like
https://www.example.com/users/123?required[]=username&required[]=password
which would give you
params = {:required => ["username", "password"]}
I would do it like so:
def show
#user = User.find_by_id(params[:id])
respond_to do |format|
format.html #default to standard 'render the show template'
format.js #default to standard 'render .js file' or whatever
format.json do
#filter out the ones we want
if params[:required]
hash = #user.attributes.slice(params[:required])
else
hash = #user.attributes
end
#this will automatically call `.to_json` on the thing we pass to it (`hash` in this case)
render json: hash
end
end
end
You can add extra security things in here, for example to limit the fields which you make accessable via the api.
Related
I have a model called Question, and it has action create;
My goal is to display a flash message instantly, using a helper method (show_alert for example) when the instance is not valid.
question_controller.rb
def create
question = Question.new(question_params)
if question.save then
redirect_to show_question_path(question.id)
else
show_alert(:warning, question.errors)
end
end
application_controller.rb
helper_method :show_alert
def show_alert(type, message)
#type = type; #msg = message
respond_to do |format|
format.js { render :template => 'alert.js.erb'}
end
end
alert.js.erb
var div = $('<div></div>').addClass(`alert alert-${#type}`)
$('<ul></ul>').append( $('<li></li>').html(#msg)
div.append(ul)
$('#alerts').html(div)
But instead of displaying the flash, I get only the partial's code on the white screen.
see the screenshot
Since I've used respond_to I got another error: ActionController::UnknownFormat
I need the snippet of code in alert.js.erb to be executed, in order to render the flash, I think the trick is somewhere in the render function, but two hours of googling were just a waste of time.
Please help! Thank you in advance
ActionController::UnknownFormat error is showing up because the browser is sending HTML request to Rails server, but the respond_to block has only specified what to do in case of a javascript request from web server.
You will need to add a little bit of Ajax to achieve what you want. See this tutorial on Ajax. https://www.tutorialspoint.com/ruby-on-rails/rails-and-ajax.htm
Ajax will send a js request to browser in the background (i.e the browser will not refresh or show any signs of loading). This js request will be sent to Rails server and it will return the .js.erb file containing the script back to the browser. Now since this script was returned as a response to Ajax request by browser, the browser will already know that it is javascript that needs to be executed.
If you do not wish to implement Ajax, you have the alternate of doing something like this in your create controller:-
def create
question = Question.new(question_params)
if question.save then
redirect_to show_question_path(question.id)
else
redirect_to new_question_path(error: question.errors) #new_question_path is the action that displays the question form to the user
end
end
and then you can initialize an error variable in the action that displays the question form. e.g.
def new
#error=params[:error]
#rest of the code...
end
And then in somewhere in your new.html.erb (or whatever the html.erb file name is)
<script>
<% if #error %>
var div = $('<div></div>').addClass(`alert alert-<%= #type %>`)
$('<ul></ul>').append( $('<li></li>').html(<%= #msg %>)
div.append(ul)
$('#alerts').html(div)
<% end %>
// you might need to tweak the variable names in controller or the above code
</script>
(This code above may not be perfect. its just to give u an idea)
However this approach will not be as quick and beautiful as ajax because when the user will submit their question, the entire page will load again to display the error warning.
By default, all output from helpers is escaped. To show the HTMl as-is, you need to use the html_safe method (https://apidock.com/rails/v4.2.1/String/html_safe). See Using helpers in a view escapes the html?
I cannot be sure this without seeing your alert.js.erb but it could be that you need to use escape_javascript in your alert.js.erb
Something like (and I haven't tested this out) in your alert.js.erb
$('<%= escape_javascript("#{type} - #{msg}") %>').appendTo("#alert")
You can read more about it on Rails Guides - Working With Javascript in Rails
Hope this helps!
Basically i'm receiving some data (variables a and h) from another site, and i would like to receive them and then make a POST with that data (the a and h variables that i already received) to my self app, i know that doesn't make a lot of sense but i need to do that.
I already coded the part where i receive the data from the other site.
MyRoute
post '/', to: "pages#info"
MyController
class PagesController < ApplicationController
skip_before_action :verify_authenticity_token, only: [:info]
protect_from_forgery :except => :info
layout "second"
def info
if params[:initzarqkr].present?
#h1 = params[:h]
#a1 = params[:a]
#h1_i = #h1.to_i
#a1_i = #a1.to_i
end
end
end
My View (shown at / where the post is received)
<h1><%= #h1 %> | <%= #a1 %> </h1>
So i want to take that data i stored on the variables #h1 and #a1, re POST it to my app and then store it on some other variables in order to show them on my view at the route /, instead of the ones that i got on the post that i received from outside (even though i know that the values should be the same ).
How can i grant this?
Thanks very much for reading.
What you'r talking about sounds like a redirect. A redirect is every time a GET request within Rails. Converting a POST to GET request has some drawbacks. You have to consider all the things which are different between a GET and POST request. One of the thing is the request lenght restriction.
In my point of view you have the following options:
redirect to the wanted action and append the parameters to the redirect url
handle the request directly within info and redirect after handling without params
store the parameters within your session hash and redirect to the other action without params
use a net/http or another libraray to post within info your data to an url and use a redirect
create a view for info with a form which gets submitted automatically via javascript
If a redirect with code 307 (repost data to new url) would work within Rails, I don't know.
I'm working on a project where I need to be able to mark certain objects for review or deletion. I'm using checkboxes, then using JavaScript to harvest the data from the checkboxes. I'm trying to use AJAX to send that data back to the Rails Controller but I keep getting a 404 error, and I'm not sure what I'm doing wrong.
This is the AJAX call (review_list and purge_list are both defined, I've checked):
function callHandleSelected() {
...
$.post('itemresults/handle_selected', { review: review_list, purge: purge_list },
function(data) {
alert(data);
});
}
And this is the route I wrote to match it:
post 'itemresults/handle_selected', to: 'processed_item#handle_selected'
I've tried adding as: :ajax into the route to see if that makes a difference without any luck.
The HTML element that calls the ajax function looks like so:
<button type="button" class="btn btn-normal" onclick="callHandleSelected()">Mark Selected as Reviewed and/or for Purge</button>
There is also a matching handle_selected method in my Ruby Controller. Every time I try to use the AJAX method I get the following error:
POST http://localhost:3000/itemresults/handle_selected 404 (Not Found) jquery.js?body=1:9667
jQuery.ajaxTransport.send jquery.js?body=1:9667
jQuery.extend.ajax jquery.js?body=1:9212
jQuery.each.jQuery.(anonymous function) jquery.js?body=1:9358
callHandleSelected processed_item.js?body=1:37
onclick
In case you need it, here is the controller method:
def handle_selected
review_list = params[:review]
purge_list = params[:purge]
review_list.each do |item|
item.split("_")
proc_item = ProcessedItem.find(item[1])
proc_item.reviewed = true;
proc_item.save!
end
purge_list.each do |item|
item.split("_")
proc_item = ProcessedItem.find(item[1])
proc_item.purge = true;
proc_item.save!
end
redirect_to processed_items_path()
#add alert
end
I think the problem is just that you need a leading slash on your request url:
$.post('itemresults/handle_selected' ...
should be
$.post('/itemresults/handle_selected'
Without the leading slash, it will add the url onto the end of the current page url.
EDIT: you should put a leading slash on the path in your routes.rb file as well. I think that rails "forgives" you for not doing this but i'm not sure: either way you should do it properly, ie with the leading slash.
A combination of the comments on my initial post answered the question. I took out the redirect_to line and replaced it with this:
respond_to do |format|
format.js {render inline: "location.reload();" }
end
I was getting the 404 error because I was trying to load objects incorrectly as Baloo pointed out. The new (relevant) code looks like this:
review_list.each do |item|
id = item.split("_")[1]
proc_item = ProcessedItem.find(id)
Thanks all!
In rails, this is an api response I'm currently generating as a response from /charts_json/south_carolina.json (for example)
[{"d_s":0,"name":"summerville"},{"d_s":1,"name":"hilton head island"},{"d_s":2,"name":"north myrtle beach"},{"d_s":1,"name":"spartanburg"},{"d_s":12,"name":"greenville, sc"},{"d_s":0,"name":"aiken"},{"d_s":6,"name":"columbia"},{"d_s":4,"name":"myrtle beach"},{"d_s":1,"name":"simpsonville"},{"d_s":1,"name":"lancaster, sc"},{"d_s":0,"name":"north augusta"},{"d_s":0,"name":"sumter"},{"d_s":0,"name":"rock hill"},{"d_s":1,"name":"beaufort, sc"},{"d_s":1,"name":"mount pleasant"},{"d_s":21,"name":"charleston"},{"d_s":1,"name":"clemson"},{"d_s":1,"name":"anderson, sc"}]
Now what I need to do is render the above like this, as a json document
[['0', 'summerville'], ['1', 'hilton head island'], ...etc... ]
For the benefit of the SO community and the clarification of the reader I'll include all the code I'm going to be using to make this work if and when I get this last thing handled
In addition to my charts_controller, I generated a charts_json_controller for responding to json requests--- an example of a controller method in that controller (this is a bit clunky but its ok for now as long as I get functionality)
def south_carolina
#locations = Location.find(1687).descendants #used acts_as_tree gem
respond_to do |format|
format.json { render json: #location.as_json(only: [:d_s, :name])}
end
In the view (cross section)
function drawMarkersMap() {
var data = google.visualization.arrayToDataTable([
['Startups', 'Location'],
$.ajax({url: '/charts_json/south_carolina', dataType: 'json'})
]);
Not sure if I'm understanding correctly, but here's a way to get the json as an array instead of a hash.
define this method for Location
class Location
def self.as_json_array
as_json(only: [:d_s, :name]).collect { |l| [l[:d_s], l[:name]] }
end
end
You could make this more general-purpose if you necessary, but I want to make sure I'm understanding your requirements first.
Then just use that method instead of as_json in your render line.
Also, it sounds like you know this, but you really should just use the same controller and put any custom code for different formats in your respond_to block.
Lately i have run into a few applications that are using js.erb and i am not really sure how to use it ...here is the code below. Can someone help me understand how this works?
in the routes.rb file
map.resources :player_emails
my controller player_emails_controller.rb in the create action
def create
#player_email = PlayerEmail.create(params[:player_email])
if #player_email.save
#response_txt = "The player has been emailed."
PlayerEmailsMailer.deliver_pattern_email(#something, #player_email, request.host_with_port)
#error = false
else
#error = true
#response_txt = "Please make sure you entered your name and a valid email address."
end
end
then i have the file player_emails/create.js.erb
$('#player_email_ind').hide();
$('#player_email_submit').show();
$('#player_response_msg').html("<%= escape_javascript #response_txt %>").fadeIn();
<% unless #error %>
$('#player_email_form')[0].reset();
<% end %>
i know what the jquery is going but i dont know how this is doing the ajax call. Does it just automatically do an ajax call when there is a js.erb...can someone explain the way this works and why i dont need a respond_to in the controller action telling it this is format.js
If a js (ajax) request is made it will respond by rendering the js.erb file and viceversa.
This is the default behaviour that is being performed:
respond_to do |format|
format.js{
render :template => 'create.js.erb'
}
format.html{
render :template => 'create.html.erb'
}
end
When the form is submitted, it does a POST to /player_emails. The resource declaration in routes.rb ensures the request is handled by PlayerEmailsController#create.
The controller is responsible for handling each format it receives. In the case of an AJAX call, the format is 'js', and is set by explicitly adding the format string to the end of the URL (/player_emails.js) or (more likely) by deducing the format from the request header.
In your case, the create action does not expect anything other than AJAX, so it takes a shortcut and omits the respond_to and format blocks. The controller has already figured out that the format is 'js', so when create is complete it takes the default action of rendering the appropriate template for the format (create.js.erb).
Does your form submit button have a :remote => true on it? If so, there might be some JavaScript in rails.js or application.js that automatically submits via AJAX. Bottom line is, there has to be some JavaScript somewhere that is making an AJAX call and asking for a js or JSON response, otherwise it would be an html request.
As for why you don't need a respond_to block, I'm not entirely sure. Maybe since the call is always being made by AJAX and there is a js.erb template available, it just does its thing without complaining. Is there an html.erb template at all? If not, try doing a regular form submit and see if it complains.