Hi i'm a beginner in Ruby on Rails Here is my doubt. I have this method in my controller
def save_profile
p "======================================"
p params
p "======================================"
p params[:company][:job_locations_attributes]
p "company_params------------------------"
p company_params
p "--------------------------------------"
company_profile
@location = JobLocation.new(:city_id => params[:city_id])
@location.save
if @company.update_attributes(company_params)
redirect_to company_dashboard_path(@company.id)
else
render 'company_profile'
end
end
and in terminal i get the output like
"======================================"
{"utf8"=>"✓", "authenticity_token"=>"kbId4JgeaM+mGlmZC1U4gFCUYN7LHmuqWq8Es3rxa+k=", "company"=>{"name"=>"Besole", "address"=>"<p>Besole, Besole, Besole, Besole</p>", "employee_count"=>"> 200", "company_type"=>"Business", "foundation_year"=>"2020", "mission"=>"Besolification", "website"=>"https://www.besole.com", "facebook_page_url"=>"https://www.facebook.com/besole", "twitter_page_url"=>"https://www.twitter.com/besole", "description"=>"Besole besole Besole Besole Besole Besole besole", "delete_logo"=>"", "logo_crop_x"=>"", "logo_crop_y"=>"", "logo_crop_h"=>"", "logo_crop_w"=>"", "jobs_attributes"=>{"33"=>{"id"=>"33", "company_id"=>"11", "title"=>"Besoler", "description"=>"<p>besoler besoler besoler</p>", "job_type_id"=>"1", "experience"=>"1 - 3 Years", "job_category_id"=>"2", "skill_ids"=>["", "6", "5", "7", "9", "11", "12", "16", "15"], "_destroy"=>"false"}}, "job_locations_attributes"=>{"33"=>{"job_locations"=>"2"}}}, "city_id"=>"40035", "portfolios_id"=>"321", "commit"=>"Submit", "controller"=>"companies", "action"=>"save_profile", "id"=>"11"}
"======================================"
{"33"=>{"job_locations"=>"2"}}
"company_params------------------------"
{"name"=>"Besole", "logo_crop_h"=>"", "logo_crop_w"=>"", "logo_crop_x"=>"", "logo_crop_y"=>"", "delete_logo"=>"", "address"=>"<p>Besole, Besole, Besole, Besole</p>", "employee_count"=>"> 200", "company_type"=>"Business", "mission"=>"Besolification", "website"=>"https://www.besole.com", "description"=>"Besole besole Besole Besole Besole Besole besole", "foundation_year"=>"2020", "facebook_page_url"=>"https://www.facebook.com/besole", "twitter_page_url"=>"https://www.twitter.com/besole", "jobs_attributes"=>{"33"=>{"id"=>"33", "company_id"=>"11", "title"=>"Besoler", "description"=>"<p>besoler besoler besoler</p>", "job_type_id"=>"1", "experience"=>"1 - 3 Years", "job_category_id"=>"2", "skill_ids"=>["", "6", "5", "7", "9", "11", "12", "16", "15"], "_destroy"=>"false"}}}
"--------------------------------------"
I want to get that 33 and Job Locations.. I want to save those two variables inside table Location under columns job_id and state_id where job_id is 33 and state_id is 2
you can use it.
job_id = params[:company][:job_locations_attributes].keys[0]
state_id = params[:company][:job_locations_attributes][job_id]['job_locations']
If you'd like to keep it short
job_id, state_id = params[:company][:job_locations_attributes].first
Location.create(:job_id => job_id, state_id => state_id.values[0])
You can get the hash values like this,
params[:company][:job_locations_attributes].keys.first => "33"
params[:company][:job_locations_attributes].values.first["job_locations"] => "2"
you can use the values to create location like this,
Location.create(:job_id => params[:company][:job_locations_attributes].keys.first, :state_id => params[:company][:job_locations_attributes].values.first["job_locations"])
Would recommend to send job_id and state_id in seperate params from your view with names like job_id and state_id , so that we can get the job_id and state_id as params[:job_id]
and params[:state_id]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.