I am part of building a ReactJS (react-on-rails) + Rails application. I am no ReactJS developer myself (yet), but was taken into the project after ReactJS chosen.
My issue regards parameters being sent from React to Rails via create and edit post requests. My models for this case:
class Hrfile < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
has_and_belongs_to_many :sras
...
end
class Sra < ActiveRecord::Base
...
has_and_belongs_to_many :hrfiles
...
validates :name, presence: {message: "Please input a name"}
end
The ReactJS uses Formsy-react as forms, where I've used and modified react-select as a multiselect form to live up to the mapping requirements of Formsy.
The following data is being sent from ReactJS through React's .post('/sras').send(data):
{
active:null
address:null
asset_id:null
assetother:null
brand_id:null
city:null
conclusion:"Test conclusion"
country_id:null
enddate:null
hrfile_ids:["2","1"]
indicator_deteroriation:null
ireport_ids:null
name:"Test assessment"
organisation_id:null
region_id:null
reviewdate:null
startdate:null
treatment:null
user_id:null
}
The problem/bug that I can't figure out, is when I create a new Sra including a name and X number of Hrfiles through the ReactJS application and post the JSON (as given above). This is received by Rails:
Started POST "/sras" for 127.0.0.1 at 2016-04-05 16:29:07 +0800
Processing by SrasController#create as JSON
Parameters: {"name"=>"Test assessment", "organisation_id"=>nil, "brand_id"=>nil, "user_id"=>nil, "asset_id"=>nil,
"startdate"=>nil, "reviewdate"=>nil, "address"=>nil, "city"=>nil, "region_id"=>nil, "country_id"=>nil, "assetother"=>nil, "enddate"=>nil, "active"=>nil, "treatment"=>nil,
"indicator_deteroriation"=>nil, "conclusion"=>"Test conclusion", "ireport_ids"=>nil, "hrfile_ids"=>["2","1"],
"sra"=>{"name"=>"Test assessment", "organisation_id"=>nil, "country_id"=>nil, "user_id"=>nil,
"startdate"=>nil, "enddate"=>nil, "reviewdate"=>nil, "active"=>nil, "city"=>nil, "address"=>nil, "asset_id"=>nil, "assetother"=>nil,
"indicator_deteroriation"=>nil, "conclusion"=>"Test conclusion", "brand_id"=>nil, "region_id"=>nil}}
The ReactJS app does forward e.g. "hrfile_ids"=>["2", "1"], however the ParamsWrapper (I suspect) does not recognize 'hrfile_ids' as an attribute and name is therefore only saved.
Anyone have an idea of what I am missing here?
Let me know if you need any other codeblocks or details. Hope you can help :) Thanks in advance -
EDIT
Thanks to Jesper by pointing out the 'sra' object was created by the params wrapper in Rails. Even though my there's a relation between the two models, Sra and Hrfile, where I can call @sra.hrfiles to get the belonging Hrfiles, I still can't figure out to get the hrfile_ids parameter wrapped into the JSON.