Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使嵌套属性在rails api only应用程序中工作_Ruby On Rails_Ruby On Rails 4_Activerecord_Devise_Rails Api - Fatal编程技术网

Ruby on rails 使嵌套属性在rails api only应用程序中工作

Ruby on rails 使嵌套属性在rails api only应用程序中工作,ruby-on-rails,ruby-on-rails-4,activerecord,devise,rails-api,Ruby On Rails,Ruby On Rails 4,Activerecord,Devise,Rails Api,我有一个只有rails api的应用程序,其中有两个模型。 我正在使用Desive_auth_令牌进行身份验证 class User < ActiveRecord::Base # attr_accessible :accounts_attributes has_many :accounts accepts_nested_attributes_for :accounts # Include default devise modules. devise :database_

我有一个只有rails api的应用程序,其中有两个模型。 我正在使用Desive_auth_令牌进行身份验证

class User < ActiveRecord::Base
  # attr_accessible :accounts_attributes
  has_many :accounts
  accepts_nested_attributes_for :accounts
  # Include default devise modules.
  devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable,
          :omniauthable
  include DeviseTokenAuth::Concerns::User
  #Deleted ":confirmable," from above
  after_create :assign_member_role

  private
  def assign_member_role
    mr = MemberRole.first # default MR
    Member.create!(user_id: self.id, member_role_id: mr.id)
  end

  def populate_account

  end

end
我正在尝试用json创建一个新用户。我想在每个用户创建的帐户模型上完成填充

如果我在注册控制器中使用以下代码

before_filter :user_params
  puts "hello"

  GET /users/sign_up
  def new

    # Override Devise default behaviour and create a profile as well
    build_resource({})
    resource.build_account
    respond_with self.resource
  end

  def create
    puts "\n\n Testing if the create method can be overriden\n\n"
    puts params.inspect
    params.require(:users).permit!
    super
  end

  protected

  def user_params
    puts "in loop\n\n\n\n"
    devise_parameter_sanitizer.for(:sign_up) { |u|
      # u.permit(:email, :password, :password_confirmation, :accounts_attributes => :school_name)
      u.permit(:email, :password, :password_confirmation, :accounts_attributes[:school_name, :line_1, :line_2, :city, :state, :country, :pincode])
    }
    # params.require(:user).permit(:email, :password, :password_confirmation, :accounts_attributes[:school_name, :line_1, :line_2, :city, :state, :country, :pincode])
  end
end
我可以创建新用户,但无法填充帐户模型。并在我的终端中获取未知参数错误和参数丢失错误,如下所示

Started POST "/auth?email=email112@meail.com&password=[FILTERED]&password_confirmation=[FILTERED]&accounts[:school_name]=mjdhs&accounts[:line_1]=asdff&accounts[:line_2]=ijdkjd&accounts[:city]=mum&accounts[:state]=mah&accounts[:country]=IN&accounts[:pincode]=400054" for 127.0.0.1 at 2015-10-07 12:50:31 +0530
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by RegistrationsController#create as */*
  Parameters: {"email"=>"email112@meail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "accounts"=>{":school_name"=>"mjdhs", ":line_1"=>"asdff", ":line_2"=>"ijdkjd", ":city"=>"mum", ":state"=>"mah", ":country"=>"IN", ":pincode"=>"400054"}}
I am called in sign_up method
I am called in sign_up method
In create
I am called in sign_up method
Completed 500 Internal Server Error in 106ms (ActiveRecord: 0.9ms)

ActiveRecord::UnknownAttributeError (unknown attribute 'controller' for User.):
  app/controllers/registrations_controller.rb:24:in `create'


  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.4ms)
  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.5ms)
  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (19.7ms)
Started POST "/auth?email=email112@meail.com&password=[FILTERED]&password_confirmation=[FILTERED]&accounts[:school_name]=mjdhs&accounts[:line_1]=asdff&accounts[:line_2]=ijdkjd&accounts[:city]=mum&accounts[:state]=mah&accounts[:country]=IN&accounts[:pincode]=400054" for 127.0.0.1 at 2015-10-07 13:01:33 +0530
hello
Processing by RegistrationsController#create as */*
  Parameters: {"email"=>"email112@meail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "accounts"=>{":school_name"=>"mjdhs", ":line_1"=>"asdff", ":line_2"=>"ijdkjd", ":city"=>"mum", ":state"=>"mah", ":country"=>"IN", ":pincode"=>"400054"}}
Unpermitted parameter: accounts
in loop





 Testing if the create method can be overriden

{"email"=>"email112@meail.com", "password"=>"password", "password_confirmation"=>"password", "accounts"=>{":school_name"=>"mjdhs", ":line_1"=>"asdff", ":line_2"=>"ijdkjd", ":city"=>"mum", ":state"=>"mah", ":country"=>"IN", ":pincode"=>"400054"}, "controller"=>"registrations", "action"=>"create"}
Completed 400 Bad Request in 2ms (ActiveRecord: 0.0ms)

ActionController::ParameterMissing (param is missing or the value is empty: users):
  app/controllers/registrations_controller.rb:65:in `create'


  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.5ms)
  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.6ms)
  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (17.3ms)

嗯,你应该有一个自己的api控制器,因为你想控制你的api。不要使用常规的控制器进行控制api@huanson那么,我是否应该放弃使用任何宝石进行身份验证呢?好吧,这取决于你和你的需要。只是说您需要自己的Api名称空间和控制器,其中Api的每个控制器都继承自您自己的Api::BaseControler(而不是应用程序\控制器)。您还希望从您的versions控制器继承,因此您可以说您发布了一个/api/v2/,其中您只更改了一个模型的某些行为,但其他所有行为仍然是v1。ryan bates在如何对API进行版本化方面有一个很好的railscast
before_filter :user_params
  puts "hello"

  GET /users/sign_up
  def new

    # Override Devise default behaviour and create a profile as well
    build_resource({})
    resource.build_account
    respond_with self.resource
  end

  def create
    puts "\n\n Testing if the create method can be overriden\n\n"
    puts params.inspect
    params.require(:users).permit!
    super
  end

  protected

  def user_params
    puts "in loop\n\n\n\n"
    devise_parameter_sanitizer.for(:sign_up) { |u|
      # u.permit(:email, :password, :password_confirmation, :accounts_attributes => :school_name)
      u.permit(:email, :password, :password_confirmation, :accounts_attributes[:school_name, :line_1, :line_2, :city, :state, :country, :pincode])
    }
    # params.require(:user).permit(:email, :password, :password_confirmation, :accounts_attributes[:school_name, :line_1, :line_2, :city, :state, :country, :pincode])
  end
end
Started POST "/auth?email=email112@meail.com&password=[FILTERED]&password_confirmation=[FILTERED]&accounts[:school_name]=mjdhs&accounts[:line_1]=asdff&accounts[:line_2]=ijdkjd&accounts[:city]=mum&accounts[:state]=mah&accounts[:country]=IN&accounts[:pincode]=400054" for 127.0.0.1 at 2015-10-07 13:01:33 +0530
hello
Processing by RegistrationsController#create as */*
  Parameters: {"email"=>"email112@meail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "accounts"=>{":school_name"=>"mjdhs", ":line_1"=>"asdff", ":line_2"=>"ijdkjd", ":city"=>"mum", ":state"=>"mah", ":country"=>"IN", ":pincode"=>"400054"}}
Unpermitted parameter: accounts
in loop





 Testing if the create method can be overriden

{"email"=>"email112@meail.com", "password"=>"password", "password_confirmation"=>"password", "accounts"=>{":school_name"=>"mjdhs", ":line_1"=>"asdff", ":line_2"=>"ijdkjd", ":city"=>"mum", ":state"=>"mah", ":country"=>"IN", ":pincode"=>"400054"}, "controller"=>"registrations", "action"=>"create"}
Completed 400 Bad Request in 2ms (ActiveRecord: 0.0ms)

ActionController::ParameterMissing (param is missing or the value is empty: users):
  app/controllers/registrations_controller.rb:65:in `create'


  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.5ms)
  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.6ms)
  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
  Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (17.3ms)