質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

2915閲覧

インスタンス変数がハッシュの時、その要素を取得したい

ymatt

総合スコア20

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2017/08/24 14:37

###前提・実現したいこと
@contactInfoの要素の一つを取得したいのですが、以下いくつか試したやり方では取得できず、nilとなってしまう、もしくはエラーとなります。この場合の正しい取得方法をご教示いただきたく、お願いいたします。

###発生している問題・エラーメッセージ

[1] pry(#<OrgPeopleController>)> @contactInfo => {"id"=>6, "address1"=>"123 Street", "address2"=>"", "city"=>"City", "postal_code"=>"12345", "email"=>"rortest2@mailinator.com", "business_number"=>"1234567890", "cell_number"=>"", "latitude"=>0.0, "longitude"=>0.0, "avatar"=>nil, "typ_contact_id"=>nil, "typ_country_id"=>124, "typ_region_id"=>526, "org_company_id"=>nil, "org_person_id"=>5, "created_at"=>Thu, 24 Aug 2017 11:32:26 UTC +00:00, "updated_at"=>Thu, 24 Aug 2017 13:12:47 UTC +00:00} [2] pry(#<OrgPeopleController>)> @contactInfo[:address1] => nil [3] pry(#<OrgPeopleController>)> @contactInfo[:email] => nil [4] pry(#<OrgPeopleController>)> @contactInfo.address1 NoMethodError: undefined method `address1' for #<Hash:0x007f8e62d229c0> from (pry):15:in `update' [5] pry(#<OrgPeopleController>)> @contactInfo[0] => nil

###該当のソースコード

class OrgPeopleController < ApplicationController def update # Create org_ca to sanitize our hash to proper "contacts" attributes @org_ca = update_person_params["org_contacts_attributes"]["0"] @org_ca[:typ_country_id] = @org_ca.delete :typ_countries @org_ca[:typ_country_id] = @org_ca[:typ_country_id][:id] @org_ca[:typ_region_id] = @org_ca.delete :typ_regions @org_ca[:typ_region_id] = @org_ca[:typ_region_id][:id] @org_ca[:org_company_id] = @org_ca.delete :org_company @org_ca[:org_company_id] = @org_ca[:org_company_id][:id] # Edit function variables, in case of failed validations and we re-render :edit @person = OrgPerson.find_by(id: current_org_person.id) #Find the OrgPerson corresponding to org_person_id FK # Using the org_person_id find/create the record and assing the attributes to @contactInfo @contactInfo = OrgContact.find_or_initialize_by(org_person_id: current_org_person.id).attributes @contactInfo[:email] = current_org_person.email @person.org_contacts.build(@contactInfo) #Build the form using @contactInfo if !@org_ca[:org_company_id].nil? OrgPerson.update(current_org_person.id ,org_company_id: @org_ca[:org_company_id]) end # Find contacts record or create them if necessary if !@contactInfo[:address1].nil? @contact = OrgContact.find_or_create_by(org_person_id: current_org_person.id) else @contact = OrgContact.find_or_create_by(org_person_id: current_org_person.id, address1: "", city: "", postal_code: "", email: "", business_number: "", latitude: 0, longitude: 0) end # Try to save it, if it saves, then redirect to the edit page with success if @contact.update_attributes(@org_ca) # If this user also controls the information of the company (i.e. COO) then also update company email OrgContact.where(email: current_org_person.email).update_all(email: @org_ca["email"]) # If there is a company, update the person record to reflect the company too. flash[:success] = "Profile updated" redirect_to edit_org_person_path(@person.id) else # Failed. Re-render the page as unsucessful render :edit end end private def update_person_params params.require(:org_person).permit(org_contacts_attributes: [:address1, :address2, :city, {typ_countries: :id}, {typ_regions: :id}, {org_company: :id}, :postal_code, :email, :business_number, :cell_number, :org_person_id, :avatar]) end end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

以下のように取得することは可能でしょうか。

ruby

1puts @contactInfo["address1"] 2puts @contactInfo["email"]

投稿2017/08/25 00:25

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ymatt

2017/08/25 04:35

""は試していませんでした。できました。ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問