0
votes

I have 3 tables

User(id,email)
Org(id,name)
Role(id,user_id,org_id,role)

Ajax request send parameters

org[name]   test
org[user][email]    [email protected]

In my controller

def create 
  client_org = Org.new(client_org_params)
  respond_to do |format|
  @client_org.valid?
  if !@client_org.errors.blank?
    format.json { render json: @client_org.errors, status: :unprocessable_entity }
    elsif Org.saveClientOrg(@client_org)

  end
end
private
def client_org_params
  params.require(:client_org).permit(:name,user_attributes: [:email])
end

In Org.model

def self.saveClientOrg(orgData)
    org=ClientOrg.new(orgData.name)//-->okay I got name: test
    orgData.user.email //error? undefined method `user' for #<Org>
end

Seem like this line code: orgData.user.email incorrect.

when I leave 2 input field blank and submit form ==> form validate says only name is blank and ignore email.Although in model user.rb I set validates :email, presence: true Anyone help me? Thanks in advance

EDIT

User.rb
has_many :roles
has_many :orgs, through: :roles
default_scope -> { order('email ASC') }
validates_format_of :email, :with  => Devise.email_regexp
validates :email, presence: true

Org.rb
has_many :roles
has_many :users, through: :roles
validates :name, presence: true , uniqueness: { case_sensitive: false }

Role.rb
belongs_to :user
belongs_to :org
1
Pls specify Associations of each Model and view calling create action of controller.Leger
i think that you can find some good hints about how to debug your problem in this article: nofail.de/2013/10/debugging-rails-applications-in-developmentphoet

1 Answers

0
votes

orgData is your org model's active record object right?

so in org.rb you need to do this

orgData.users.email

user should be plural users