为什么我的加入无法正常工作?

灰色光谱

用户有一个配置文件。配置文件属于用户。该查询似乎在postgres中工作,但是我无法使关联在视图中正常工作。查询结果如下:

  User Load (0.4ms)  SELECT "users".* FROM "users"   ORDER BY "users"."id" ASC
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 5 ORDER BY "users"."id" ASC LIMIT 1
  Profile Load (0.3ms)  SELECT  "profiles".* FROM "profiles"  WHERE "profiles"."user_id" = $1 LIMIT 1  [["user_id", 5]]

这是用户和个人资料的两种模型:

用户:

class User < ActiveRecord::Base

   devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :confirmable

   has_one :profile, dependent: :destroy

   accepts_nested_attributes_for :profile, allow_destroy: true

end

轮廓:

class Profile < ActiveRecord::Base

   belongs_to :user, polymorphic: true

end

控制器...

用户:

class UsersController < ApplicationController

   def index
      @profiles = Profile.all
      @users = User.all.order(:id)
      User.joins(:profile)
   end

   private
      def set_user
         @user = User.find(params[:id])
      end

 end

轮廓:

class ProfilesController < ApplicationController
   before_action :set_profile, only: [:show, :edit, :update, :destroy]

   # GET /profiles
   # GET /profiles.json
   def index
      @profiles = Profile.all
   end

   # GET /profiles/1
   # GET /profiles/1.json
   def show
   end

   # GET /profiles/new
   def new
      @profile = Profile.new
   end

   # POST /profiles
   # POST /profiles.json
   def create
      @profile = Profile.new(profile_params)

      respond_to do |format|
         if @profile.save
            format.html { redirect_to @profile, notice: 'Profile was successfully created.' }
            format.json { render :show, status: :created, location: @profile }
         else
            format.html { render :new }
            format.json { render json: @profile.errors, status: :unprocessable_entity }
         end
      end
   end

 private

   def set_profile
      @profile = Profile.find(params[:id])
   end

   def profile_params
      params.require(:profile).permit(:user_id)
   end

结尾

最后在/ users / index视图中:

<%= user.profile.user_id %>

但是,当我尝试渲染视图时,出现以下错误:

nil:NilClass的未定义方法`user_id'

感谢您的帮助。

内尔敏

如果您真的不需要关联是多态的,则应将其从Profile模型中删除

class Profile < ActiveRecord::Base

   belongs_to :user
end

但是,如果您确实需要关联是多态的,则需要更改User模型。

class User < ActiveRecord::Base

   devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :confirmable

   has_one :profile,
      dependent: :destroy,
      as: user

   accepts_nested_attributes_for :profile, allow_destroy: true

end

还需要添加user_type在你的Profile模型

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我的Jekyll液体“加入”标签不能正常工作?

来自分类Dev

为什么我的__clone()无法正常工作?

来自分类Dev

为什么我的ViewBag无法正常工作?

来自分类Dev

为什么我的DATEPART无法正常工作?

来自分类Dev

为什么我的闹钟无法正常工作?

来自分类Dev

为什么我的ButtonClickListerner无法正常工作?

来自分类Dev

为什么我的.hover()无法正常工作?

来自分类Dev

为什么我的BXSlider无法正常工作?

来自分类Dev

为什么我的ButtonClickListerner无法正常工作?

来自分类Dev

为什么我的功能无法正常工作

来自分类Dev

为什么我的clearfix无法正常工作?

来自分类Dev

为什么我的加载无法正常工作

来自分类Dev

为什么我的SQL无法正常工作?

来自分类Dev

为什么我的代码无法正常工作?

来自分类Dev

为什么我的fadeIn无法正常工作?

来自分类Dev

为什么我的Ajax无法正常工作?

来自分类Dev

我无法删除文件,rm无法正常工作,为什么?

来自分类Dev

为什么我的降低元素高度的算法无法正常工作

来自分类Dev

为什么我的悬停功能无法正常工作?

来自分类Dev

为什么我的CSS选择器无法正常工作?

来自分类Dev

为什么我的Gradle安装无法正常工作?

来自分类Dev

为什么我的简单angularjs无法正常工作?

来自分类Dev

为什么我的checkbox.change无法正常工作(jquery)?

来自分类Dev

为什么我的Jpanel borderlayout无法正常工作?

来自分类Dev

为什么我在jquery中的focus()无法正常工作?

来自分类Dev

为什么我的canvas clearRect无法正常工作?

来自分类Dev

为什么我的CSS无法在Java中正常工作?

来自分类Dev

为什么我的条件格式无法正常工作?

来自分类Dev

为什么我的基本默认.acceptbutton无法正常工作?

Related 相关文章

  1. 1

    为什么我的Jekyll液体“加入”标签不能正常工作?

  2. 2

    为什么我的__clone()无法正常工作?

  3. 3

    为什么我的ViewBag无法正常工作?

  4. 4

    为什么我的DATEPART无法正常工作?

  5. 5

    为什么我的闹钟无法正常工作?

  6. 6

    为什么我的ButtonClickListerner无法正常工作?

  7. 7

    为什么我的.hover()无法正常工作?

  8. 8

    为什么我的BXSlider无法正常工作?

  9. 9

    为什么我的ButtonClickListerner无法正常工作?

  10. 10

    为什么我的功能无法正常工作

  11. 11

    为什么我的clearfix无法正常工作?

  12. 12

    为什么我的加载无法正常工作

  13. 13

    为什么我的SQL无法正常工作?

  14. 14

    为什么我的代码无法正常工作?

  15. 15

    为什么我的fadeIn无法正常工作?

  16. 16

    为什么我的Ajax无法正常工作?

  17. 17

    我无法删除文件,rm无法正常工作,为什么?

  18. 18

    为什么我的降低元素高度的算法无法正常工作

  19. 19

    为什么我的悬停功能无法正常工作?

  20. 20

    为什么我的CSS选择器无法正常工作?

  21. 21

    为什么我的Gradle安装无法正常工作?

  22. 22

    为什么我的简单angularjs无法正常工作?

  23. 23

    为什么我的checkbox.change无法正常工作(jquery)?

  24. 24

    为什么我的Jpanel borderlayout无法正常工作?

  25. 25

    为什么我在jquery中的focus()无法正常工作?

  26. 26

    为什么我的canvas clearRect无法正常工作?

  27. 27

    为什么我的CSS无法在Java中正常工作?

  28. 28

    为什么我的条件格式无法正常工作?

  29. 29

    为什么我的基本默认.acceptbutton无法正常工作?

热门标签

归档