Ruby on Rails保存到数据库

时间:2014-03-20 06:41:52

标签: sql ruby-on-rails ruby

嗨,我是Ruby on Rails的初学者 这是我的疑问。 我在我的控制器中有这个方法

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

并在终端i中获得类似

的输出
"======================================"
{"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"}}}
"--------------------------------------"

我想得到33和工作地点.. 我想在job_id和state_id列下的表位置中保存这两个变量,其中job_id为33且state_id为2

3 个答案:

答案 0 :(得分:3)

你可以使用它。

job_id = params[:company][:job_locations_attributes].keys[0] 

state_id = params[:company][:job_locations_attributes][job_id]['job_locations']

答案 1 :(得分:2)

如果你想保持简短

job_id, state_id = params[:company][:job_locations_attributes].first

Location.create(:job_id => job_id, state_id => state_id.values[0])

答案 2 :(得分:1)

你可以得到像这样的哈希值,

params[:company][:job_locations_attributes].keys.first => "33" 
params[:company][:job_locations_attributes].values.first["job_locations"] => "2"

您可以使用这些值来创建这样的位置,

Location.create(:job_id => params[:company][:job_locations_attributes].keys.first, :state_id => params[:company][:job_locations_attributes].values.first["job_locations"])

建议使用 job_id state_id 之类的名称从视图中的单独参数中发送job_id和state_id,以便我们可以将job_id和state_id作为{{1 }和params[:job_id]