【问题标题】:How to add current_user to user_id to form_for in rails?如何在rails中将current_user添加到user_id到form_for?
【发布时间】:2012-10-06 12:11:29
【问题描述】:

我有一个用于创建材料的表格(标题、描述和内容 - 都是基本的)。表单很好地保存了这些详细信息,但它没有保存 user_id,它应该是 current_user 的 user_id。我该怎么做呢?这一定很简单,但到目前为止没有任何效果。

def create 
   @material = Material.new(params[:material])
   if @material.save
     flash[:success] = "Content Successfully Created"
     redirect_to @material
   else
     render 'new'
   end
end

【问题讨论】:

    标签: ruby-on-rails form-for


    【解决方案1】:
    def create 
     @material = Material.new(params[:material])
     @material.user_id = current_user.id if current_user
     if @material.save
      flash[:success] = "Content Successfully Created"
      redirect_to @material
     else
      render 'new'
     end
    end
    

    【讨论】:

      【解决方案2】:

      有几种不同的方法可以做到这一点,具体取决于您的应用程序设置方式。如果用户和材料之间存在关系(用户有很多材料),您可以在控制器中使用它:

      def create
        @material = current_user.materials.new(params[:material])
        # ...
      end
      

      如果您没有这种关系,我仍然建议将其设置在控制器中,而不是表单中的隐藏字段。这样会更安全,因为它不会让任何人篡改用户 ID 值:

      def create
        @material = Material.new(params[:material].merge(user_id: current_user))
        # ...
      end
      

      【讨论】:

      • 是的,我一直在考虑设置关联。我退缩了,因为虽然一个用户确实创建了材料,但有时可能会有多个用户创建材料,而我还不想处理这个问题。不过谢谢!
      【解决方案3】:

      假设您将登录用户的对象保存在 current_user 中,以下将适用于您

         @material = Material.new(params[:material])
         @material.user_id = current_user.id
         if @material.save
      

      【讨论】:

        【解决方案4】:

        在创建对象之前使用 Rails 5 和参数需要 permitted,这是将 current_user 合并到参数中的最简单方法,感谢 @Peter Brown 在他的回答中:

        def create
          @discussion = current_user.materials.new(new_material_params)
          # ...
        end
        
        private
        def new_material_params
          params.require(:material).permit(:title, :description,: content)
        end
        

        如果您使用accepts_nested_attributes_for创建嵌套对象,则需要手动深度合并到关联参数中:

        class User < ApplicationRecord
          has_many :discussions # Used to associate User with Discussion later
        end
        
        class Comment < ApplicationRecord
          belongs_to :user
        end
        
        class Discussion < ApplicationRecord
          belongs_to :user
          has_many :comments
          accepts_nested_attributes_for :comments
        end
        
        class DiscussionsController < ApplicationController
          def create
            # Merge the params[:discussion][:user_id] by using the relationship's #new
            @discussion = current_user.discussion.new(new_discussion_params)
          end
        
          private
          # Sanitized params for creation, not editing
          def new_discussion_params
            params.require(:discussion)
              .permit(:title, :user_id, 
                      comments_attributes: [:id, :content, :discussion_id, :user_id])
              .tap do |discussion_params|
                # Require the association parameters, and if they exist,
                # set :user_id for each.
                discussion_params.require(:comments_attributes).each do |i, comment|
                  comment.merge!(user_id: current_user.id)
                end
            end
          end
        end
        

        注意:设置(或覆盖!)params[:discussion][:comments_attributes]["0"][:user_id] 的内容非常适合创建。但是,如果除了创建之外,您还允许编辑深层层次结构,请确保您不会意外地用当前用户覆盖所有:user_ids。

        【讨论】:

          猜你喜欢
          • 2017-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多