Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/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 Rails 4,Desive&;多态关联_Ruby_Devise_Ruby On Rails 4_Polymorphic Associations - Fatal编程技术网

Ruby Rails 4,Desive&;多态关联

Ruby Rails 4,Desive&;多态关联,ruby,devise,ruby-on-rails-4,polymorphic-associations,Ruby,Devise,Ruby On Rails 4,Polymorphic Associations,下面是我通过Rails 4 JSON API注册名为mentor的用户类型的功能代码 现在我在想,有没有更好的办法?一种更干净/更简单的方法,Rails可以自动创建用户/导师关联 目前,我正在create方法中手动设置它,这似乎不正确。所以我只是想确定没有更好的办法来解决这个问题 型号/用户.rb class User < ActiveRecord::Base # Include default devise modules. Others available are: # :co

下面是我通过Rails 4 JSON API注册名为mentor的用户类型的功能代码

现在我在想,有没有更好的办法?一种更干净/更简单的方法,Rails可以自动创建用户/导师关联

目前,我正在
create
方法中手动设置它,这似乎不正确。所以我只是想确定没有更好的办法来解决这个问题

型号/用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  belongs_to :role, :polymorphic => true
end
class Mentor < ActiveRecord::Base
  has_one :user, as: :role
  accepts_nested_attributes_for :user
end
class Api::V1::MentorsController < ApplicationController
  respond_to :json

  def create
    @user = User.new(user_params)
    @mentor = Mentor.new(mentor_params)
    @user.role = @mentor
    @user.save!
    @mentor.user_id = @user.id
    @mentor.save!
    respond_with :api, @mentor
  end

  private

  def mentor_params
    params.require(:mentor).permit(:first_name, :last_name)
  end

  def user_params
    params.require(:user).permit(:email, :password)
  end
end
class Api::V1::MentorsController < ApplicationController
  respond_to :json

  def create
    @mentor = Mentor.new(mentor_params)
    @mentor.user.save!
    @mentor.user_id = @mentor.user.id
    @mentor.save!
    respond_with :api, @mentor
  end

  private

  def mentor_params
    params.require(:mentor).permit(:first_name, :last_name, user_attributes: [:email, :password])
  end
end
class用户true
结束
models/mentor.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  belongs_to :role, :polymorphic => true
end
class Mentor < ActiveRecord::Base
  has_one :user, as: :role
  accepts_nested_attributes_for :user
end
class Api::V1::MentorsController < ApplicationController
  respond_to :json

  def create
    @user = User.new(user_params)
    @mentor = Mentor.new(mentor_params)
    @user.role = @mentor
    @user.save!
    @mentor.user_id = @user.id
    @mentor.save!
    respond_with :api, @mentor
  end

  private

  def mentor_params
    params.require(:mentor).permit(:first_name, :last_name)
  end

  def user_params
    params.require(:user).permit(:email, :password)
  end
end
class Api::V1::MentorsController < ApplicationController
  respond_to :json

  def create
    @mentor = Mentor.new(mentor_params)
    @mentor.user.save!
    @mentor.user_id = @mentor.user.id
    @mentor.save!
    respond_with :api, @mentor
  end

  private

  def mentor_params
    params.require(:mentor).permit(:first_name, :last_name, user_attributes: [:email, :password])
  end
end
class-Mentor
控制器/api/V1/mentors\u controller.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  belongs_to :role, :polymorphic => true
end
class Mentor < ActiveRecord::Base
  has_one :user, as: :role
  accepts_nested_attributes_for :user
end
class Api::V1::MentorsController < ApplicationController
  respond_to :json

  def create
    @user = User.new(user_params)
    @mentor = Mentor.new(mentor_params)
    @user.role = @mentor
    @user.save!
    @mentor.user_id = @user.id
    @mentor.save!
    respond_with :api, @mentor
  end

  private

  def mentor_params
    params.require(:mentor).permit(:first_name, :last_name)
  end

  def user_params
    params.require(:user).permit(:email, :password)
  end
end
class Api::V1::MentorsController < ApplicationController
  respond_to :json

  def create
    @mentor = Mentor.new(mentor_params)
    @mentor.user.save!
    @mentor.user_id = @mentor.user.id
    @mentor.save!
    respond_with :api, @mentor
  end

  private

  def mentor_params
    params.require(:mentor).permit(:first_name, :last_name, user_attributes: [:email, :password])
  end
end
类Api::V1::导师控制器
更新-10/01/2013

我在这方面取得了一些进展。以下是我现在拥有的:

控制器/api/V1/mentors\u controller.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  belongs_to :role, :polymorphic => true
end
class Mentor < ActiveRecord::Base
  has_one :user, as: :role
  accepts_nested_attributes_for :user
end
class Api::V1::MentorsController < ApplicationController
  respond_to :json

  def create
    @user = User.new(user_params)
    @mentor = Mentor.new(mentor_params)
    @user.role = @mentor
    @user.save!
    @mentor.user_id = @user.id
    @mentor.save!
    respond_with :api, @mentor
  end

  private

  def mentor_params
    params.require(:mentor).permit(:first_name, :last_name)
  end

  def user_params
    params.require(:user).permit(:email, :password)
  end
end
class Api::V1::MentorsController < ApplicationController
  respond_to :json

  def create
    @mentor = Mentor.new(mentor_params)
    @mentor.user.save!
    @mentor.user_id = @mentor.user.id
    @mentor.save!
    respond_with :api, @mentor
  end

  private

  def mentor_params
    params.require(:mentor).permit(:first_name, :last_name, user_attributes: [:email, :password])
  end
end
类Api::V1::导师控制器

但是我仍然需要手动设置用户id。仅执行
Mentor.create(Mentor\u params)
无法设置用户id。有什么办法可以解决这个问题吗?

这是我的想法,但基本思想是这样的

使用嵌套资源创建窗体

form_for @mentor do |f|
  f.input :mentor_val
  f.fields_for :user do |m|
    m.input :user_val
它应该以如下格式发布params对象:

mentor: {
  mentor_val: 'blah'
  user_attributes: {
    user_val: 'foo'
  }
}
现在,由于您已经在Mentor模型中包含了
accepts\u nested\u attributes\u for
,Rails自动向
Mentor
添加了一个
user\u attributes=
方法,该方法将构建用户模型,包括设置关系。这意味着要创建这两个模型,只需在控制器中调用

@mentor.create(params)

要回答更新中的问题,请尝试将id添加到params白名单中:

def mentor_params
    params.require(:mentor).permit(:first_name, :last_name,user_attributes: [:id, :email, :password])
end

您是否尝试在用户中创建
mentor=
方法?Rails应该获取params[:user][:mentor]并将其传递给
mentor=
方法。然后你可以构建你的Mentor.new并在你的用户类中完成所有其他的事情。您的控制器应该变成just
@user=user.new(user\u params)
@user.save
用api、@user.mentor
@jeremywoertink响应\我不想把所有东西都放在一个用户控制器中。我将有两个不同的用户类型与不同的领域。所以我宁愿将逻辑分离到它们自己的控制器中。我实际上尝试过这种方法,但它完全忽略了用户参数,并且没有在两者之间创建任何关联。类似的经验。我意识到我在请求中命名的属性是错误的。因此,你的建议奏效了,但只是部分奏效(见上文)。