仅在保存param时保存之前分配值

时间:2018-02-03 15:57:03

标签: ruby-on-rails

我只是在没有为该参数传递密钥的情况下尝试在保存记录时分配值。我尝试了两种不同的方式,每种方式都将值分配给未分类的'即使通过了类别参数。在这两种情况下,我希望分配当前用户,并且用户永远不会传递不同的值(这是有效的)。

我有两个问题:(1)为什么值总是设置为未分类,即使它已通过,(2)控制器是此操作的最佳位置?

尝试1(也尝试使用.has_key?(:category)

def create
  @entry = Wiki.new
  check_and_assign_values
if @entry.save(wiki_params)
...
def wiki_params
  params.require(:wiki).permit(:content, :category, files: [])
end

def check_and_assign_values
  @entry.created_or_modified_by = "#{current_user.first_name} #{current_user.last_name}"
  @entry.category = 'uncategorized' unless params[:category].present?
end

尝试2(我认为如果传递,param可能会覆盖):

def create
  @entry = Wiki.new(
    created_or_modified_by:"#{current_user.first_name} #{current_user.last_name}",
    category: 'uncategorized')
if @entry.save(wiki_params)
...
def wiki_params
  params.require(:wiki).permit(:content, :category, files: [])
end

编辑:我认为问题的根源可能实际上是其他地方。使用以下代码,类别参数作为哈希的一部分传递,但永远不会插入到DB中:

def create
  @entry = Wiki.new(merged_params)
  if @entry.save!
    flash[:success] = 'Your article has been created'
    redirect_to proc { wiki_url(@entry) }
  else
    flash[:error] = @entry.errors.full_messages.each { |e| e }
  end
end

private

def wiki_params
  params.require(:wiki).permit(:content, :category, files: [])
end

def user_full_name
  "#{current_user.first_name} #{current_user.last_name}"
end

def merged_params
  @category = (params[:category] || 'uncategorized')
  wiki_params.merge!(created_or_modified_by: user_full_name, category: @category)
end

控制台结果:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"S2lV2r3WysQpjn4b9OTgbBMIqYzMSQPdEgwtiCvTKya/fEpP5DqraMgUDDdi+VXJKNB5l0E0Qf9iM8k9JDuFhQ==", "wiki"=>{"content"=>"ital content *ital*", "category"=>"cate"}, "commit"=>"submit"}
...
Wiki Create (3.0ms)  INSERT INTO "wikis" ("created_or_modified_by", "content", "markdown_content", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["created_or_modified_by", "Jane doe"], ["content", "ital content *ital*"], ["markdown_content", "<p>ital content <em>ital</em></p>\n"], ["created_at", "2018-02-04 12:55:32.499462"], ["updated_at", "2018-02-04 12:55:32.499462"]]
  ↳ app/controllers/wikis_controller.rb:15

2 个答案:

答案 0 :(得分:2)

试试这个

def create
  @entry = Wiki.new(wiki_params)
  @entry.category = 'uncategorized' if @entry.blank?
  @entry.created_or_modified_by = current_user.full_name
  if @entry.save
    flash[:success] = 'Your article has been created'
    redirect_to proc { wiki_url(@entry) }
  else
    flash.now[:error] = @entry.errors.full_messages
    render :new
  end
end

private

def wiki_params
  params.require(:wiki).permit(:content, :category, files: [])
end

这在您的用户模型中:

class User < ActiveRecord::Base
  ...

  def full_name
    "#{first_name} #{last_name}"
  end
end

答案 1 :(得分:1)

check_and_assign_values方法中的代码存在问题。 应该是这样的 def check_and_assign_values @entry.created_or_modified_by = "#{current_user.first_name} #{current_user.last_name}" @entry.category = 'uncategorized' unless params[:wiki][:category].present? end 检查访问类别参数的方式。