Rails 4在控制器未运行时起作用

时间:2014-03-28 15:24:22

标签: activerecord ruby-on-rails-4 controller simple-form has-many-through

我有一些我正在处理的Rails 4项目的代码。它使用active_record(mysql2),当我通过has_many :through(在生产或开发中)进行交互时,有一个rails c关系可以正常工作。当我尝试以表格形式提交关系时(我使用的是simple_form),我似乎无法将其保存起来。

以下是我目前如何设置信息的方式(只显示片段,我无法真正展示整个来源):

型号:

has_many :categorizations
has_many :resource_categories, through: :categorizations

accepts_nested_attributes_for :resource_categories
accepts_nested_attributes_for :categorizations

形式:

= simple_form_for @resource do |f|
  = f.association :resource_categories

控制器:

# POST /resources
# POST /resources.json
def create
  @resource = Resource.new(resource_params)
  set_categories(@resource, params[:resource][:resource_category_ids])

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

# PATCH/PUT /resources/1
# PATCH/PUT /resources/1.json
def update
  respond_to do |format|
    if @resource.update(resource_params)
      set_categories(@resource, params[:resource][:resource_category_ids])
      format.html {
        redirect_to @resource, notice: 'Resource was successfully updated.'
      }
      format.json {
        head :no_content
      }
    else
      format.html {
        render action: 'edit'
      }
      format.json {
        render json: @resource.errors, status: :unprocessable_entity
      }
    end
  end
end

# Never trust parameters from the scary internet, only allow the white list
# through.
def resource_params
  params.require(:resource).permit(
    :title, :slug, :ancestry, :status, :author_id, :published, :parent_id,
    :resource_category_ids, :preview, :body
  )
end

def set_categories(resource, categories)

  # Clean out the existing categories (if there are any)
  unless resource.resource_categories.blank?
    resource.resource_categories.each do |category|
      resource.resource_categories.delete(category)
    end
  end

  unless categories.blank?
    categories.each do |category|
      unless category.blank?
        resource.resource_categories << ResourceCategory.find(category)
      end
    end
  end
end

当我使用rails c -e production(或只是普通rails c)发出以下命令时,它可以正常工作(在本例中,我将所有类别分配给所有资源):

Resource.all.each do |resource|
  ResourceCategory.all.each do |category|
    resource.resource_categories << category
  end
end

似乎我的问题是控制器没有调用辅助函数

1 个答案:

答案 0 :(得分:1)

请改用:

def create
  @resource = Resource.new(resource_params)    
  @resource.set_categories(params[:resource][:resource_category_ids])
  ..
end 

Resource模型中移动方法:

def set_categories(categories)

  # Clean out the existing categories (if there are any)
  unless new_record?
    unless resource_categories.blank?
      resource_categories.each do |category|
        resource_categories.delete(category)
      end
    end
  end

  unless categories.blank?
    categories.each do |category|
      unless category.blank?
        resource_categories << ResourceCategory.find(category)
      end
    end
  end
end

@resource是Controller的实例变量,您无需将其传递给方法。直接在实例变量上执行所有操作。

OP在保存记录时仍然存在问题,在:resource_category_ids方法中将:resource_category_ids => []更改为resource_params

def resource_params 
params.require(:resource).permit( 
:title, :slug, :ancestry, :status, :author_id, :published, :parent_id, 
 :preview, :body, :resource_category_ids => [] 
) 
end