Renaming parameters in nested resources - ruby-on-rails-4

In Rails 4.2.6, I am creating nested resources like this
routes.rb:
resources :analysis, param: :project_id do
resources :configuration
end
resources :decision, param: :project_id do
resources :configuration
end
It works fine when I am in the analysis or decision controller. However, when I am in the configuration controller, I receive the param :analysis_project_id or :decision_project_id. How can I change this so I still receive :project_id?
I don't want to customize my configuration controller to be aware that it is nested in different resources.

just this, you need not to send params over here, It will take automatically with corresponding to your action.
resources :analysis do
resources :configuration
end
if you need routes like these
analysis_configuration_index GET /analysis/:analysis_id/configuration(.:format) configuration#index
POST /analysis/:analysis_id/configuration(.:format) configuration#create
new_analysis_configuration GET /analysis/:analysis_id/configuration/new(.:format) configuration#new
edit_analysis_configuration GET /analysis/:analysis_id/configuration/:id/edit(.:format) configuration#edit
analysis_configuration GET /analysis/:analysis_id/configuration/:id(.:format) configuration#show
PATCH /analysis/:analysis_id/configuration/:id(.:format) configuration#update
PUT /analysis/:analysis_id/configuration/:id(.:format) configuration#update
DELETE /analysis/:analysis_id/configuration/:id(.:format) configuration#destroy
analysis_index GET /analysis(.:format) analysis#index
POST /analysis(.:format) analysis#create
new_analysis GET /analysis/new(.:format) analysis#new
edit_analysis GET /analysis/:id/edit(.:format) analysis#edit
analysis GET /analysis/:id(.:format) analysis#show
PATCH /analysis/:id(.:format) analysis#update
PUT /analysis/:id(.:format) analysis#update
DELETE /analysis/:id(.:format) analysis#destroy
EDITED 13 Aug 2020
To get the specific param in nested resources
resources :analysis, param: :project_id do
member do
resources :configurations
end
end
resources :decision, param: :project_id do
member do
resources :configurations
end
end
Expected routes
configurations GET /analysis/:project_id/configurations(.:format) configurations#index
POST /analysis/:project_id/configurations(.:format) configurations#create
new_configuration GET /analysis/:project_id/configurations/new(.:format) configurations#new
edit_configuration GET /analysis/:project_id/configurations/:id/edit(.:format) configurations#edit
configuration GET /analysis/:project_id/configurations/:id(.:format) configurations#show
PATCH /analysis/:project_id/configurations/:id(.:format) configurations#update
PUT /analysis/:project_id/configurations/:id(.:format) configurations#update
DELETE /analysis/:project_id/configurations/:id(.:format) configurations#destroy
analysis_index GET /analysis(.:format) analysis#index
POST /analysis(.:format) analysis#create
new_analysis GET /analysis/new(.:format) analysis#new
edit_analysis GET /analysis/:project_id/edit(.:format) analysis#edit
analysis GET /analysis/:project_id(.:format) analysis#show
PATCH /analysis/:project_id(.:format) analysis#update
PUT /analysis/:project_id(.:format) analysis#update
DELETE /analysis/:project_id(.:format) analysis#destroy
GET /decisions/:project_id/configurations(.:format) configurations#index
POST /decisions/:project_id/configurations(.:format) configurations#create
GET /decisions/:project_id/configurations/new(.:format) configurations#new
GET /decisions/:project_id/configurations/:id/edit(.:format) configurations#edit
GET /decisions/:project_id/configurations/:id(.:format) configurations#show
PATCH /decisions/:project_id/configurations/:id(.:format) configurations#update
PUT /decisions/:project_id/configurations/:id(.:format) configurations#update
DELETE /decisions/:project_id/configurations/:id(.:format) configurations#destroy
decisions GET /decisions(.:format) decisions#index
POST /decisions(.:format) decisions#create
new_decision GET /decisions/new(.:format) decisions#new
edit_decision GET /decisions/:project_id/edit(.:format) decisions#edit
decision GET /decisions/:project_id(.:format) decisions#show
PATCH /decisions/:project_id(.:format) decisions#update
PUT /decisions/:project_id(.:format) decisions#update
DELETE /decisions/:project_id(.:format) decisions#destroy

Related

Rails running methods inappropriately

A rails controller defines a before_action
before_action :set_intervento, only: [ ... ]
to
def set_intervento
#intervento = Intervento.find(params[:id])
end
But my action risultati_fatturati is not listed in the only block. yet error Couldn't find Intervento with 'id'=risultati_fatturatiis being generated where Rails justifying itself as having to run the set_intervento method. What else is at work here that is launching the method?
EDIT
routes show
resources :interventos do
collection do
get :indexmio
get :indexmio_programmati
get :passati
get :non_assegnati
get :programmati
get :domani
get :domani_e_dopo
get :settimana
get :planning
get :in_corso
get :successivi
get :successivo
get :passati_da_eseguire
get :articolo_adhoc
get :calendar
post :calendarc
get :settimanale
get :chiusi
post :ripresa
get :risultati_articolos
get :risultati_ordina_articolos
get :creare_checklist
post :neuter_ddt
post :cancel_ddt
get :elenco_ddt
get :dispatcher_check
get :manager_list
get :manager_comments
post :manager_check_required
get :preventivi_attivi
get :cerca_fatturati
get :non_fatturabili
post :risultati_fatturati
get :cerca_chiusi
post :risultati_chiusi
post :duplicate_intervento
get :ordinari_chiusi
get :ordinari_aperti
end
member do
get :pdfc
get :create_pdf_rapporto
get :refresh_pause_intervento
get :refresh_close_intervento
patch :update_successivo
patch :update_ddt
patch :materials_sucessivo
patch :materials_availability
patch :preventivare
patch :esito_preventivo
patch :scaduto_preventivo
patch :ripristina_preventivo
get :dispatcher_fix
patch :dispatcher_signature
get :correzione_fattura
end
end
Translating to
indexmio_interventos GET /interventos/indexmio(.:format) interventos#indexmio
indexmio_programmati_interventos GET /interventos/indexmio_programmati(.:format) interventos#indexmio_programmati
passati_interventos GET /interventos/passati(.:format) interventos#passati
non_assegnati_interventos GET /interventos/non_assegnati(.:format) interventos#non_assegnati
programmati_interventos GET /interventos/programmati(.:format) interventos#programmati
domani_interventos GET /interventos/domani(.:format) interventos#domani
domani_e_dopo_interventos GET /interventos/domani_e_dopo(.:format) interventos#domani_e_dopo
settimana_interventos GET /interventos/settimana(.:format) interventos#settimana
planning_interventos GET /interventos/planning(.:format) interventos#planning
in_corso_interventos GET /interventos/in_corso(.:format) interventos#in_corso
successivi_interventos GET /interventos/successivi(.:format) interventos#successivi
successivo_interventos GET /interventos/successivo(.:format) interventos#successivo
passati_da_eseguire_interventos GET /interventos/passati_da_eseguire(.:format) interventos#passati_da_eseguire
articolo_adhoc_interventos GET /interventos/articolo_adhoc(.:format) interventos#articolo_adhoc
calendar_interventos GET /interventos/calendar(.:format) interventos#calendar
calendarc_interventos POST /interventos/calendarc(.:format) interventos#calendarc
settimanale_interventos GET /interventos/settimanale(.:format) interventos#settimanale
chiusi_interventos GET /interventos/chiusi(.:format) interventos#chiusi
ripresa_interventos POST /interventos/ripresa(.:format) interventos#ripresa
risultati_articolos_interventos GET /interventos/risultati_articolos(.:format) interventos#risultati_articolos
risultati_ordina_articolos_interventos GET /interventos/risultati_ordina_articolos(.:format) interventos#risultati_ordina_articolos
creare_checklist_interventos GET /interventos/creare_checklist(.:format) interventos#creare_checklist
neuter_ddt_interventos POST /interventos/neuter_ddt(.:format) interventos#neuter_ddt
cancel_ddt_interventos POST /interventos/cancel_ddt(.:format) interventos#cancel_ddt
elenco_ddt_interventos GET /interventos/elenco_ddt(.:format) interventos#elenco_ddt
dispatcher_check_interventos GET /interventos/dispatcher_check(.:format) interventos#dispatcher_check
manager_list_interventos GET /interventos/manager_list(.:format) interventos#manager_list
manager_comments_interventos GET /interventos/manager_comments(.:format) interventos#manager_comments
manager_check_required_interventos POST /interventos/manager_check_required(.:format) interventos#manager_check_required
preventivi_attivi_interventos GET /interventos/preventivi_attivi(.:format) interventos#preventivi_attivi
cerca_fatturati_interventos GET /interventos/cerca_fatturati(.:format) interventos#cerca_fatturati
non_fatturabili_interventos GET /interventos/non_fatturabili(.:format) interventos#non_fatturabili
risultati_fatturati_interventos POST /interventos/risultati_fatturati(.:format) interventos#risultati_fatturati
cerca_chiusi_interventos GET /interventos/cerca_chiusi(.:format) interventos#cerca_chiusi
risultati_chiusi_interventos POST /interventos/risultati_chiusi(.:format) interventos#risultati_chiusi
duplicate_intervento_interventos POST /interventos/duplicate_intervento(.:format) interventos#duplicate_intervento
ordinari_chiusi_interventos GET /interventos/ordinari_chiusi(.:format) interventos#ordinari_chiusi
ordinari_aperti_interventos GET /interventos/ordinari_aperti(.:format) interventos#ordinari_aperti
pdfc_intervento GET /interventos/:id/pdfc(.:format) interventos#pdfc
create_pdf_rapporto_intervento GET /interventos/:id/create_pdf_rapporto(.:format) interventos#create_pdf_rapporto
refresh_pause_intervento_intervento GET /interventos/:id/refresh_pause_intervento(.:format) interventos#refresh_pause_intervento
refresh_close_intervento_intervento GET /interventos/:id/refresh_close_intervento(.:format) interventos#refresh_close_intervento
update_successivo_intervento PATCH /interventos/:id/update_successivo(.:format) interventos#update_successivo
update_ddt_intervento PATCH /interventos/:id/update_ddt(.:format) interventos#update_ddt
materials_sucessivo_intervento PATCH /interventos/:id/materials_sucessivo(.:format) interventos#materials_sucessivo
materials_availability_intervento PATCH /interventos/:id/materials_availability(.:format) interventos#materials_availability
preventivare_intervento PATCH /interventos/:id/preventivare(.:format) interventos#preventivare
esito_preventivo_intervento PATCH /interventos/:id/esito_preventivo(.:format) interventos#esito_preventivo
scaduto_preventivo_intervento PATCH /interventos/:id/scaduto_preventivo(.:format) interventos#scaduto_preventivo
ripristina_preventivo_intervento PATCH /interventos/:id/ripristina_preventivo(.:format) interventos#ripristina_preventivo
dispatcher_fix_intervento GET /interventos/:id/dispatcher_fix(.:format) interventos#dispatcher_fix
dispatcher_signature_intervento PATCH /interventos/:id/dispatcher_signature(.:format) interventos#dispatcher_signature
correzione_fattura_intervento GET /interventos/:id/correzione_fattura(.:format) interventos#correzione_fattura
interventos GET /interventos(.:format) interventos#index
POST /interventos(.:format) interventos#create
new_intervento GET /interventos/new(.:format) interventos#new
edit_intervento GET /interventos/:id/edit(.:format) interventos#edit
intervento GET /interventos/:id(.:format) interventos#show
PATCH /interventos/:id(.:format) interventos#update
PUT /interventos/:id(.:format) interventos#update
DELETE /interventos/:id(.:format) interventos#destroy
and the controller action
def risultati_fatturati
date = params[:intervento]
#date_start = Date.civil(date["dal(1i)"].to_i, date["dal(2i)"].to_i, date["dal(3i)"].to_i)
#date_end = Date.civil(date["al(1i)"].to_i, date["al(2i)"].to_i, date["al(3i)"].to_i)
if !params[:intervento][:invoicestate_id].blank?
#interventos = Intervento.joins(:signatures).where(['signatures.created_at >= ? AND signatures.created_at <= ? AND signatures.sequence = ? AND interventos.invoicestate_id = ?', #date_start.at_beginning_of_day, #date_end.at_end_of_day, "fine", params[:intervento][:invoicestate_id]]).paginate :page => params[:page], :per_page => 100
else
#interventos = Intervento.joins(:signatures).where(['signatures.created_at >= ? AND signatures.created_at <= ? AND signatures.sequence = ?', #date_start.at_beginning_of_day, #date_end.at_end_of_day, "fine"]).paginate :page => params[:page], :per_page => 100
end
render layout: "application"
end
so the action is within a collection and does not call #intervento. This is why I find it strange.
Edit #2
Side note: attempted to remove the before_action and find the member in each action, leading to identical results. Thus there is a deeper cause.
In an attempt to view things differently and understand via triangulation, I attempted a theoretically simpler process. That of running a partial for updating each record
<%= render "stato_fatturazione", intervento: intervento %>
whose form calls a specific action, which will invoke Rails's ujs
<%= form_for stato_fatturazione_intervento_path(intervento), method: :patch, remote: true, data: {disable_with: "Aggiornando..."} do |f| %>
Alas, notwithstanding the parameters, that have the proper path for editing, rails is attempting to patch the page with the array of records, which is by definition unpatchable, and leads to the same Couldn't find Intervento with 'id'=risultati_fatturati
Started PATCH "/interventos/risultati_fatturati" for ::1 at 2017-07-31 10:52:14 +0200
Processing by InterventosController#update as JS
Parameters: {"utf8"=>"✓", "/interventos/123/stato_fatturazione"=>{"invoicestate_id"=>"3", "in_zucchetti"=>"475", "fatturato"=>"1"}, "commit"=>"Aggiorna", "id"=>"risultati_fatturati"}
The rendered HTML does confirm the target action is being intercepted by some method:
<form data-disable-with="Aggiornando..." action="/interventos/risultati_fatturati" accept-charset="UTF-8" data-remote="true" method="post">

Rails 4.2.5 can't hide routes after adding new collection route

I have a model with only two actions: list the index (watch all changes since last check) and delete all the items in the table.
As such I define a new action for the delete (in config/routes.rb):
resources :changes do
collection { delete 'delete_all' }
end
This works fine (after creating the delete_all action in the controller).
Next step is to restrict the routes that are exposed, because I only need index and the new one:
resources :changes, :only => [ :index, :delete_all ]
Unfortunately, even with this second line, the resources aren't constrained at all.
If I don't define the new action delete_all, it does work as expected.
resources :changes, :only => [ :index, :delete_all ], work with only curd action like [new,index,edit,show,destroy,create,update].
So rails resources: not allow to add custom action in ":only =>[]",and you will use with the collection.
OK I found it. Actually Rails makes a difference between the "resourceful" and "non-resourceful" routes. The resource full ones are the 7 default ones and a lot of methods only work on those.
Finally I defined the routes in as following and this worked fine:
get 'changes/' => 'changes#index', as: 'changes'
delete 'changes/' => 'changes#delete_all', as: 'delete_all'

VCR is not recording cassettes on successful requests, only on failed ones

I have a simple test to fetch one Facebook object. I'm using Curl for the request.
it "gets an object from Facebook" do
VCR.use_cassette('facebook') do
url = "https://graph.facebook.com/<ID>?access_token=#{#access_token}&#{query_string}"
curl = Curl::Easy.perform(url)
expect(curl.body_str).to eql('<my object>')
end
end
My VCR configs are:
VCR.configure do |c|
c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
c.hook_into :webmock
end
When I run the tests, it passes, and the following is logged:
[Cassette: 'facebook'] Initialized with options: {:record=>:once, :match_requests_on=>[:method, :uri], :allow_unused_http_interactions=>true, :serialize_with=>:yaml, :persist_with=>:file_system}
[webmock] Handling request: [get https://graph.facebook.com/<ID>?access_token=<TOKEN>&fields=%5B%22id%22,%22account_id%22,%22name%22,%22campaign_group_status%22,%22objective%22%5D] (disabled: false)
[Cassette: 'facebook'] Initialized HTTPInteractionList with request matchers [:method, :uri] and 0 interaction(s): { }
[webmock] Identified request type (recordable) for [get https://graph.facebook.com/<ID>?access_token=<TOKEN>&fields=%5B%22id%22,%22account_id%22,%22name%22,%22campaign_group_status%22,%22objective%22%5D]
But the cassette is not recorded and the dir is empty. I've tried :record => :all to same results.
Usually, people encountered this error when using incompatible hooks for the library they're using, but that's not the case. I'm using webmock and curb.
Curiously, the cassette is recorded when there's a failure in the request, e.g., the token is expired. When it's fixed, and I delete the file, it's not recorded again.
Have anyone had the same problem?
It turns out that my code was a little more complicated than above and was executing a callback after perfoming the request. Something like:
success_handler = Proc.new { return c.body_str }
curl.on_success do |easy|
success_handler.call(easy)
end
That bypasses VCR and the file is not written. Refactoring the code to not use callbacks works.

Grails: pass a List from GSP to controller with remote link

So, in my method A in the controller ServicioComunitario I send this to the GSP:
tg = ServicioComunitario.findAll("from ServicioComunitario as b where "+query)
[servicioComunitarioInstanceList: tg, params: params]
Then in the GSP I call another method (generarDocDeReporte) of ServicioComunitarioController:
<g:set var="b" value="${'xls'}"/>
<g:set var="a" value="${'excel'}"/>
<g:set var="servicioLista" value="${servicioComunitarioInstanceList}"/>
<g:link controller="ServicioComunitario" action="generarDocDeReporte"
params="${[exportFormat:a, extesion:b, tg: servicioLista] }"
update="mainContent">Excel</g:link><br/>
Then, in the new method "generarDocDeReporte" I have:
println params.exportFormat+"-"+params.extesion
if(params.tg)
println "Not empty"
exportFormat and extension work as expected, but the params.tg doesn't seem to behave normal.
I am trying to use this new params.tg where it was a ServicioComunitario.list(params):
exportService.export(params.exportFormat, response.outputStream, ServicioComunitario.list(params), fields, labels, formatters, parameters)
And here is where I get the error:
exportService.export(params.exportFormat, response.outputStream, params.tg, fields, labels, formatters, parameters)
When I receive the params.tg, do I need to cast it? or what do you think is the error?
Thank you very much in advance
You can't just pass a list of instances like that in a link. You can however collect the ids into a list as a parameter and then use it to populate it later. For example:
<g:link controller="ServicioComunitario" action="generarDocDeReporte"
params="${[exportFormat:a, extesion:b, tgids: servicioLista.collect{it.id}.join(',')] }"
update="mainContent">Excel</g:link><br/>
And then in your controller where you need to get the list again:
def tg = ServicioComunitario.getAll(params?.tgids?.tokenize(","))
Also, you don't need to assign params to params when returning your model. parameters are already exposed in the GSP by convention.
[servicioComunitarioInstanceList: tg]

changing ember RESTAdapter get request

at the moment, when ember is asking for child data through the rest adapater, it makes a GET request with the following options:
http://localhost/orders?ids%5B%5D=0x0000000000000386&ids%5B%5D=0x00000000000003a4&ids%5B%5D=0x00000000000003cf&ids%5B%5D=0x0000000000000631&ids%5B%5D=0x0000000000000639
which equates to parameters of
ids[]:0x0000000000000386
ids[]:0x00000000000003a4
ids[]:0x00000000000003cf
ids[]:0x0000000000000631
ids[]:0x0000000000000639
I was wondering if there was a way of changing that to be either
id1:0x0000000000000386
id2:0x00000000000003a4
id3:0x00000000000003cf
id4:0x0000000000000631
id5:0x0000000000000639
or
{ids: [{"id":"0x0000000000000386"},
{"id":"0x00000000000003a4"},
{"id":"0x00000000000003cf},"
{"id":"0x0000000000000631"},
{"id":"0x0000000000000639"}
]}
I have solved this by using the "links" option in the data.
Within the json returned at the higher level , include the links
{customers : [
{name": "foobar inc",
"links": {"orders:/customers/181/orders"}
}]
}
so now when ember tries to get the orders of a customer, it will make a json request to the url specified in the links
this works really well for me. It also has the advantage of not having to load all children in as either ids[] or sideloading.