使用创建的对象

时间:2018-02-14 10:27:30

标签: ruby-on-rails parameters relational-database

我在使用rails应用程序中创建的对象保存当前用户时遇到问题。

我跟着迈克尔哈特尔导轨教程直到第10章(我不需要第11章到第14章的以下功能。)从那里起,我为儿童和幼儿园制作了一些支架,并编辑了模型中的关系(例如用户有很多孩子,孩子属于用户)。目的是获得一个应用程序,如果用户被分配了父母角色,用户可以创建他或她的孩子,或者如果用户被分配了幼儿园经理角色,则用户可以创建幼儿园。之后,应用程序应该帮助将所有注册的孩子分配到所有注册的幼儿园。

我当前的问题是,用户(此时没有角色,只是常规用户)无法在Web界面中创建子项,因为在尝试保存子项时它说“用户必须存在”,因为我认为没有用户被分配给孩子。 不幸的是我不知道如何将当前用户保存到孩子身上。我发现了一个非常相似的问题here我尝试按照答案但我无法解决问题,而是现在我遇到了错误。 我将.merge(user: current_user)部分编辑到子控制器,但它给了我错误:

“ChildrenController中的ActiveModel :: MissingAttributeError #create 无法写出未知属性user_id

    # POST /children.json
    def create
      @child = Child.new (child_params)
      @child.save

模型user.rb和child.rb:

    #app/models/child.rb
    class Child < ApplicationRecord
      belongs_to :user
      validates :user, presence: true
    end

    #app/models/user.rb
    class User < ApplicationRecord
      attr_accessor :remember_token, :activation_token, :reset_token
      has_many :children, dependent: :destroy
      has_many :kindergartens, dependent: :destroy
    ....
    end

children_controller.rb:

    #app/controllers/children_controller.rb
    class ChildrenController < ApplicationController
      before_action :set_child, only: [:show, :edit, :update, :destroy]

    ....

      def create
         @child = Child.new child_params
         @child.save

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

    ....

       def child_params
         params.require(:child).permit(:firstname, :lastname, :postalcode, :city, :street, :addr_number, :gender, :disability, :allday, :halal, :koscher, :vegetarian, :vegan).merge(user: current_user)
       end
    end

1 个答案:

答案 0 :(得分:0)

首先,您需要将user_id索引添加到childrens表。

然后

如果您有登录&amp;注销功能,我的意思是会话当前正在工作,那么你如何管理用户会话?像这样的东西?

def current_user
  @current_user ||= User.find_by(id: session[:user_id])
end

如果是,则转到create方法并按以下方式进行编辑

@child = Child.new child_params
@child.user = current_user
@child.save

.merge(user: current_user)中移除此child_params,我希望它能正常工作。

如果上述解决方案无法以某种方式工作,则手动传递user_id表格,如表格

<%= f.hidden_field :user_id, value: current_user.id %>

然后user_id会像其他属性一样添加强参数。

你可以看到测试,就像user_id正确传递然后发生的事情一样

仅用于测试目的

@child = Child.new child_params
@child.user = User.last
@child.save

第2部分

如果您需要授予children_controller权限,如果用户有父级,则他/她可以访问children_controller表单,然后创建一个这样的方法

before_action :require_parants, only: [:new, :create] # top of the controller
private
# parant column on the users table is boolean true/false
def require_parants
    if !logged_in? || (logged_in? and !current_user.parant?)
        flash[:danger] = "Only parants can create child"
        redirect_to root_url
    end
end

logged_in?基于此

# Returns true if the user is logged in, false otherwise.
def logged_in?
  !current_user.nil?
end

然后在用户不是父级时重定向到根URL。

希望有所帮助

相关问题