1

をレール:CRUDメソッドは、私がレールに新たなんだと私はここに非常に基本的な何かが欠落している可能性があり

ユーザーは、支店や会社

の部門の両方のための連絡先を作成することができますBranch.rb

class Branch < ApplicationRecord 
    belongs_to :company 
    has_many :contacts 
end 

Division.rb

class Division < ApplicationRecord 
    belongs_to :company 
    has_many :contacts 
end 

Contact.rb

class Contact < ApplicationRecord 
    belongs_to :branch 
    belongs_to :division 
end 

今、ユーザーがそこにはdivision_idはありませんし、課のページから連絡先を作成することができ、分岐ページから連絡先を作成することができます。

が、私はこのように私のroutes.rbをを定義している:

routes.rbを

resources :companies, :shallow => true do 
    get 'company_page' 
    resources :branches, :shallow => true do 
     get 'branch_page' 
     resources :contacts 
    end 
    resources :divisions, :shallow => true do 
     get 'division_page' 
     resources :contacts 
    end 
end 

をその結果、私は支店や部門のいずれかから連絡先を作成する場合、それは、連絡先番号に行きますメソッドを作成します。私contacts_controller.rbで

、私が持っている:

def create 
    @newContact = Contact.new(contact_params) 
    id = @division = @branch = nil 
    isBranch = false 
    if params[:branch_id] != nil 
     isBranch = true 
     id = params[:branch_id] 
    else 
     isBranch = false 
     id = params[:division_id] 
    end 
    if isBranch 
     branch = Branch.find(id) 
     @newContact.branch = branch 
     @branch = branch 
    else 
     division = Division.find(id) 
     @newContact.division = division 
     @division = division 
    end 

    respond_to do |format| 
     if @newContact.save 
      format.js 
      format.html { render :nothing => true, :notice => 'Contact created successfully!' } 
      format.json { render json: @newContact, status: :created, location: @newContact } 
     else 
      format.html { render action: "new" } 
      format.json { render json: @newContact, status: :unprocessable_entity } 
     end 
    end  
end 

しかし、私は@newContact.save中にActiveRecord Errorに直面しています。

ここでは根本的に非常に間違ったことをやっていると確信しています。レールは私が知らない別のエレガントな方法でそのようなことを処理します。

+0

エラーは何:

def create @new_contact = Contact.new(contact_params) if @new_contact.save branch = @new_contact.branch division = @new_contact.division redirect_path = branch ? branch_path(branch) : division_path(division) respond_to do |format| format.js format.html { redirect_to redirect_path, :notice => 'Contact created successfully!' } format.json { render json: @new_contact, status: :created, location: @new_contact } end else respond_to do |format| format.html { render action: "new" } format.json { render json: @new_contact, status: :unprocessable_entity } end end end 

これは、それが動作することを証明していますか? – Anthony

+0

'265msで500の内部サーバーエラーを完了しました(ActiveRecord:66。8ms) ' – user122121

+0

私は、「連絡先」に2つの関連があるが、あなたはそれを1つだけ与えているので、エラーを推測しています。 log/development.logのエラーは何ですか? – Anthony

答えて

3

@Anthonyが指摘したように、オプションのごbelongs_to関連付けを作成する必要があります:

# app/models/contact.rb 

class Contact < ApplicationRecord 
    belongs_to :branch, optional: true 
    belongs_to :division, optional: true 
end 

しかし、別の問題がparams[:division_id]params[:branch_id]は常にゼロであるということです。どちらのキーも[:contact]キーの中にあります。だからあなたが取得しているエラーはActiveRecord::RecordNotFound: Couldn't find Division with 'id'=

この条件ロジックはすべて必要ありません。あなたは、どんなパラメータが与えられても、新しい連絡先を作ることができます。また、変数命名にRubyの規約を使用する必要があります。これはcamelCaseではなくsnake_caseです。

最後に、関連付けられているものに応じて、ブランチページまたは部門ショーページのいずれかにHTMLリクエストをリダイレクトするとします。だから私はそれを行うためのロジックを追加しました。

ここで、コントローラ#create行動の迅速なリファクタリングです:

# spec/controllers/contacts_controller_spec.rb 

require 'rails_helper' 

RSpec.describe ContactsController, type: :controller do 
    let(:company) { Company.create!(name: 'Company Name') } 
    let(:division) { Division.create!(name: 'Division Name', company: company) } 
    let(:branch) { Branch.create!(name: 'Branch Name', company: company) } 

    describe '#create' do 
    context 'when created with a division id' do 
     let(:attributes) { {'division_id' => division.id, 'name' => 'Contact Name'} } 

     it 'creates a contact record and associates it with the division' do 
     expect(Contact.count).to eq(0) 
     post :create, params: {contact: attributes} 

     expect(Contact.count).to eq(1) 
     contact = Contact.first 
     expect(contact.division).to eq(division) 
     end 
    end 

    context 'when created with a branch id' do 
     let(:attributes) { {'branch_id' => branch.id, 'name' => 'Contact Name'} } 

     it 'creates a contact record and associates it with the branch' do 
     expect(Contact.count).to eq(0) 
     post :create, params: {contact: attributes} 

     expect(Contact.count).to eq(1) 
     contact = Contact.first 
     expect(contact.branch).to eq(branch) 
     end 
    end 
    end 
end 
+0

ありがとう!それが私の疑問をすべて解決しました。 – user122121

関連する問題