ajax get data from a rails controller - ruby-on-rails

I am trying to use ajax to get information from my controller. Basically I want to know if a data already exists in the DB or not, so my controller will return either true or false.
At the moment I am just trying to set the basic ajax call
in my js file I have the following ajax call, as you can see at the moment I am not really doing any logic because my data is just a placeholder. Later I will add the information I want to query
$.ajax({
type: "GET",
url: "/locations/exists",
dataType: "JSON",
data: { 'locition_exist': loc_exit },
success: function(data) {
console.log(data);
}
});
In my controller I have
def exists
location_exists = true
respond_to do |format|
format.html
format.json {render json: location_exists }
end
end
Later value will go into a method in the model that will query the DB and return true/false.
In my routes I have
resources :locations
get 'locations/exists', to: 'locations#exists'
This code results in the following error
The action 'show' could not be found for LocationsController
I am new to rails and ajax , and I based my code on different examples that I read here, so I am probably just doing a stupid noob mistake.
Thank you very much for your help

When using get or match in routes, you need to define the controller method mapped
get 'locations/value', to: 'locations#value'
update
You saw the same error after updating routes. There are two reasons of this error:
You defined resources :location at first. The url locations/exists itself matches "show" method within resources, with Routes taking 'exists' as the id in #show.
You have not defined show within LocationsController
So, Routes will firstly map the url to locations#show with the :id as 'exists', then hits the controller and found #show does not exist.
The solution after your updating
Of course you can put get 'exists'... before resources but that looks ugly.
Since 'exists' requires no id, it is a collection method. So you can use Routes built-in ways to do that.
resources :locations do
collection do
get 'exists'
end
end
By this all your resources and 'exists' can live.

Related

How I can use param in url without creating table in database

I have a controller that works without a table in the database.
At the moment, there is an action index that performs a request to the api from which it performs the action. Route looks like /car_collector.
And my question is how to create a route /car_collector/:id/edit without creating table in database.
Just creating a route get '/car_collector/:id/edit', to: 'car_collector#edit', as: 'edit_doc_collector', param: :id an error is generated ActiveRecord::StatementInvalid in Admin::CarCollectorController#edit
resources :car_collector, only: :edit
That's it, you don't need do sometning extra
After that just go to /car_collector/1/edit, /car_collector/2/edit etc.
Default param will be as params[:id]

Rails 5 Custom Controller Action for CSV Export

I am using Ruby's built in CSV functionality to export some tables as csv files. It all works fine if I am in the
def index
// code
end
However, I have a custom action in the controller called 'annual' with a 'respond_to block as follows:
respond_to do |format|
format.html # index.html.erb
format.json { render json: #surveys }
format.csv { send_data #surveys.to_csv, filename: "survey-annual-#{Date.today}.csv" }
end
The error is:
"status":404,"error":"Not Found","exception":"#\u003cActiveRecord::RecordNotFound: Couldn't find Survey with 'id'=annual\u003e","traces":{"Application Trace":[{"id":1,"trace":"app/controllers/surveys_controller.rb:34:in `show'"}]
I don't believe this is specific to .csv as .json produces the same error. I am wondering if defining a method '.to_csv' in the model is causing a problem because of Ruby class structure; I'm not good enough with Ruby and haven't been able to find an answer.
Thanks!
Its actually a very basic and common routing error and has nothing to do with CSV generation. Check the logs if you don't belive me.
Routes in Rails have priority in the order they are defined. So if you for example have declared your routes as:
resources :surveys
get 'surveys/annual', to: 'surveys#annual'
The request for surveys/annual will always be passed to SurveysController#show as it matches first.
Routes are just superpowered regexes. So the GET /surveys/:id route does not merely look for integer ids.
Rather the following will match:
/surveys/1
/surveys/foo
/surveys/foo%20bar%20baz
But not /surveys/foo/bar or POST /surveys/foo.
To add additional restful actions properly you should instead use a block and the collection option:
resources :surveys do
get :annual, on: :collection
end
This adds the route in the correct order as seen below:
Prefix Verb URI Pattern Controller#Action
annual_surveys GET /surveys/annual(.:format) surveys#annual
surveys GET /surveys(.:format) surveys#index
POST /surveys(.:format) surveys#create
new_survey GET /surveys/new(.:format) surveys#new
edit_survey GET /surveys/:id/edit(.:format) surveys#edit
survey GET /surveys/:id(.:format) surveys#show
PATCH /surveys/:id(.:format) surveys#update
PUT /surveys/:id(.:format) surveys#update
DELETE /surveys/:id(.:format) surveys#destroy
See:
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

Error adding custom route to controller

I've rewritten my question to be more accurate. I have a bankaccounts controller / model.
I have the following method on my controller
def search
##ledgeritems = Ledgeritem.where("bankaccount_id = ? and transactiondate >= ? and transactiondate < ?", params[:bankaccount_id], params[:startdate], params[:enddate])
#bankaccount = Bankaccount.find(params[:bankaccount_id])
respond_to do |format|
format.js { render :partial => "bankaccount/bankledger" }
end
end
I've made two attempts to call this.
Attempt 1
Route for attempt 1
resources :bankaccounts do
post "search"
end
This shows the following route when I do a rake
bankaccount_search POST /bankaccounts/:bankaccount_id/search(.:format) bankaccounts#search
Javascript for calling attempt 1
$.ajax({
type: "POST",
url: "/bankaccounts/" + bank_account_id + "/search.js",
data: $('#edit_bankaccount_' + bank_account_id).serialize(),
success: function (result, status) {
$('#bank_ledger_div').html(result);
}
});
This calls the correct route on my controller, but the server sees it as a PUT instead of a POST and returns a 404.
Attempt 2
Route for attempt 2
resources :bankaccounts do
collection do
post "search"
end
end
This shows the following route when I do a rake
search_bankaccounts POST /bankaccounts/search(.:format) bankaccounts#search
Javascript for calling attempt 2
$.ajax({
type: "POST",
url: "/bankaccounts/search.js",
data: $('#edit_bankaccount_' + bank_account_id).serialize(),
success: function (result, status) {
$('#bank_ledger_div').html(result);
}
});
This calls the update route but still showing as a PUT command. In Firebug I see a 500 error with the following result
Couldn't find Bankaccount with id=search
Usually this error means you're making a GET request instead of a POST request.
For example:
GET /bankaccounts/search is assumed to be requesting the SHOW page for a bankaccount with ID = search
While
POST /bankaccounts/search would correctly hit your action.
Edit:
resources :bankaccounts do
collection do
post "search"
end
end
Is correct as well. Now I'm noticing that you are doing this to get your data:
data: $('#edit_bankaccount_' + bank_account_id).serialize()
that form likely has a hidden field in it, put there by rails, with name='_method' and value='PUT'. That is what is convincing rails that your POST is really a PUT. You'll need to remove that from the serialized data in order to correctly post the form.
If you want the /search url to be used without specifying an id, you should declare it as a collection action :
resources :bankaccounts do
collection do
post "search"
end
end
You can check the routes defined in your app with the rake routes command, to ensure that you defined what you meant.
The URL is expecting the format
/bankaccounts/:bankaccount_id/search
Is the error coming from this method? Could /bankaccounts/search be matching another route?

Add RESTful Action

The source of my information is section 2.9 here:
[http://guides.rubyonrails.org/routing.html#connecting-urls-to-code][1]
What I'm trying to do is add a custom action "search" and corresponding view.
So, as it says to do in the documentation, I've added this code in my config/routes.rb file:
resources :dimensions do
collection do
get "search"
end
end
I've also defined in the dimensions_controller file:
def search
#dimensions = Dimension.all
respond_to do |format|
format.html # search.html.erb
format.json { render json: #dimensions }
end
end
I then stopped and restarted the rails server, but when I navigate to /dimensions/home, I'm still getting this error message:
Couldn't find Dimension with id=search
Also showing that my parameter is:
{"id"=>"search"}
So am I just missing another bit of code that gives the instruction to interpret /dimension/search as a collection action as opposed to the show action?
I've already confirmed that search_dimensions_path exists, so I know that the resource block in the routes.rb file is actually adding paths. It's just interpreting them as a separate search action that's giving me trouble.
Thanks for your time.
This code should work fine. Can you show us your routes.rb file?
On a side note, you probably don't want to have a separate action for searching, using the index action is the preferred way.
Found the issue:
I had to make the resource declaration in my config/routes.db file for dimensions after creating the collection action, like so:
resources :dimensions do
collection do
get "search"
end
end
resources :dimensions
Then everything worked as expected.

rails post request

I'm not quite understanding how requests are handled in rails, in my controller, I have something like this:
def method_to_handle_request
# do stuff
end
Then in the view I have this JavaScript:
$.post("/", {method_to_handle_request: "value"}, function(data) {
// do something with data
});
This is wrong: data in the JavaScript ends up just being the page. Thus, my question is: how do I handle requests in rails? I've been reading "Agile Web Development With Rails", and the section there doesn't make too much sense to me.
Thanks
Rails uses configured routes to point to the appropriate controller action. You have to specify a route in your config/routes.rb file to send your request through the desired action. In your controller, you've defined the method_to_handle_request. You have to make sure that you define a route for that. There are many ways to do this within the routes.rb file and those are well documented in this guide.
Then in your method_to_handle_request you should render/format your output as JSON or XML or whatever you want your view to consume. If you don't do this you'll end up just getting the flat templated HTML back. Here's a good resource for rendering views that has a section on JSON in particular.
So here's a quick example of what I mean:
in config/routes.rb
resources :foos do
member do
post 'do_my_foo'
end
end
in foos_controller.rb
def do_my_foo
foo = Foo.find(params[:id])
# do some things to poor ole foo
some_cool_data_about_foo = ...
respond_to do |format|
format.json { render :json => some_cool_data_about_foo }
end
end
Then when you call it with ajax you'll get the JSONified data in your handler.
Geeze, I hope this is what you were asking...

Resources