2

Note: I've read a couple posts similar to this. But non of the solutions works for me.
I have two objects Appeal and Appealer with one to many relationship. When I save the Appeal object all fields are saved and also the appealer id is saved (as FK). But in the Appealer table a new record is saved with no data except for id.

MODEL

class Appeal < ActiveRecord::Base
    belongs_to :appealer, :autosave => true
    accepts_nested_attributes_for :appealer
end

class Appealer < ActiveRecord::Base
    has_many :appeals, :autosave => true
end

AppealsController

class AppealsController < ApplicationController
    def index
        respond_with Appeal.all
    end

    def create
        appealer = Appealer.create(appealer_params)
        @appeal = Appeal.create(appeal_params)
        @appeal.appealer = appealer


        if @appeal.save
          respond_with @appeal
        else
          respond_with {:error}
        end
    end

    def show
        respond_with Appeal.find(params[:id])
    end

    private
    def appeal_params
        params.require(:appeal).permit(:subject, :status, :submit_means, :card_type, :submit_date, :relationship, :report_date, :explanation, :email_approval)
    end
    def appealer_params
        params.require(:appeal).permit(appealer: [:appealer_id, :first_name, :last_name])
    end
end

EDIT Here is the json I use

{
        "id": 21,
        "subject": "axxxscaaaa",
        "status": "happy",
        "submit_means": "DOAR",
        "card_type": "sdsd",
        "submit_date": 1466629200000,
        "relationship": null,
        "report_date": 1466542800000,
        "explanation": "sdsd",
        "email_approval": null,
        "appealer": {"first_name":"aaaaaaa", "last_name":"fffff"},
        "selfRequest": false,
        "created_at": 1465851600000,
        "updated_at": 1465333200000
    }

I don't understand why the fields of appealer are not saved

7
  • have you gonna through railscasts.com/episodes/… Commented Jul 4, 2016 at 9:12
  • Yes I already went through this example and it didn't worked. Commented Jul 4, 2016 at 9:16
  • 2
    Can you use appealer_attributes in appealer_params? Like this params.require(:appeal).permit(appealer_attributes: [:appealer_id, :first_name, :last_name]). And I believe you are already using fields_for for appealer? Commented Jul 4, 2016 at 9:22
  • You might want to post your form view Commented Jul 4, 2016 at 9:43
  • Are you receiving the params on the controller request? What are your params after the permit ? Commented Jul 4, 2016 at 9:49

2 Answers 2

2
  • As you JSON object showing, you are not using rails fields_for or something on view form. So you don't need this( accepts_nested_attributes_for :appealer ) line in your model. Model should be like this:

      class Appeal < ActiveRecord::Base
        belongs_to :appealer, :autosave => true
      end
    
  • Next thing, in your current logic,for your appealer_params, change appealer_params method with this:

    def appealer_params
      params.require(:appealer).permit(:first_name, :last_name)
    end
    
  • Create action logic for your scenario:

    def create
      @appeal = Appeal.new(appeal_params)
      if appealer_params.present?
        appealer = Appealer.create(appealer_params)      
        @appeal.appealer = appealer
      end
    
      if @appeal.save
        respond_with @appeal
      else
        respond_with {:error}
      end
    end
    
Sign up to request clarification or add additional context in comments.

5 Comments

and leave the rest of the code as is? If I do that I get an error "param is missing or the value is empty: appealer"
Yes in you current scenario, you can leave the code as it is. But if it possible to receive blank values for appealer's data or no appealer data. Then you need to handle it . Give me exact scenario then I will update my answer.
The appealer can be null (no data at all) but if there is appealer then all 3 fields must be with values.
I still get error "param is missing or the value is empty: appealer". What am I missing?
0

Try this

def appeal_params
   params.require(:appeal).permit(:subject, :status, :submit_means, :card_type, :submit_date, :relationship, :report_date, :explanation, :email_approval, appealers_attributes: [:id, :first_name, :last_name] )
end

and remove this line appealer = Appealer.create(appealer_params)

1 Comment

Ok i got. This is the issue of field_for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.