繁体   English   中英

在Rails中处理多对多助手视图的正确方法是什么?

[英]what is the proper way to handle many to many helper views in rails?

我需要在创建/更新表单的每个字段旁边添加一个选择框,以建立其来源。 有一个包含摄影师信息的表,一个包含源的表以及一个在它们之间建立多对多关系的表。 关系将采用摄影师的ID,该记录中的列(例如,姓氏为“ last”)和该信息来自的来源的ID(表中的数据可以很好地来自不同的来源) -粒度方式)。

在摄影师的创建/更新表单中的每个字段旁边添加选择框以选择信息来源的最佳方法是什么?

我正在将Ruby on Rails 3.0.3与mysql2连接器一起使用。

现在,这是我必须显示给定列的源的代码段(不完整,因为缺少显示列名的方法):

Source: <%= collection_select("photographers_sources","source_id",Source.all, :id, :name, {:include_blank => 'None'})%><br />

但是我不知道如何:

  1. 预选择其id与多对多表的source_id匹配的源
  2. 发回该数据( columnsource_id )并在数据库中执行关联

我的架构:

ActiveRecord::Schema.define(:version => 0) do

  create_table "photographers", :force => true do |t|
    t.string  "first"
    t.string  "last"
    ...
  end

  create_table "photographers_sources", :force => true do |t|
    t.string  "photographer_column", :limit => 32, :default => "", :null => false
    t.integer "source_id",                                         :null => false
    t.integer "photographer_id",                                   :null => false
    t.string  "extra"
  end

  add_index "photographers_sources", ["photographer_id"], :name => "photographer_id"
  add_index "photographers_sources", ["source_id"], :name => "source_id"

  create_table "sources", :force => true do |t|
    t.string "name"
  end

end

我的模特:

class Photographer < ActiveRecord::Base
  validates :first,  :presence => true
  has_many :photographers_sources, :dependent => :destroy
  has_many :sources, :through => :photographers_sources

  def sourcelist
    PhotographersSource.where(:photographer_id => id)
  end
end

class Source < ActiveRecord::Base
  validates :name,  :presence => true
  has_many :photographers_sources, :dependent => :destroy
  has_many :photographers, :through => :photographers_sources
end

class PhotographersSource < ActiveRecord::Base
  belongs_to :photographer
  belongs_to :source
  validates :photographer_column,  :presence => true
  validates :source_id, :presence => true,
                        :numericality => { :only_integer => true }
  validates :photographer_id, :presence => true,
                        :numericality => { :only_integer => true }
end

我为源添加了带有选择框的部分表单:

<%
if @photographer.sourcelist.select{|s| s.photographer_column==column}.length != 0
    @id_to_select = @photographer.sourcelist.select{|s| s.photographer_column==column}[0].source_id
else
    @id_to_select = 0
end
%>

Source: <%= collection_select("photographers_sources_plain", column, Source.all, :id, :name, {:include_blank => 'None', :selected => @id_to_select })%><br />

像这样将其放置在每个字段旁边:

<%= render (:partial => 'source_form', :locals => {:column => 'category'}) %>

您将“类别”更改为任何字段名称。 我敢肯定必须有更好的方法,但这对我来说已经足够了。

请注意,在选择中生成的name是“ photographers_sources_plain [column]”,因此在控制器中我具有update操作:

params[:photographer][:photographers_sources] = []
plain_sources = params[:photographers_sources_plain]
plain_sources.each do |key, value|
  if value != ""
    params[:photographer][:photographers_sources] << PhotographersSource.new(:photographer_column=>key,:source_id=>value.to_i)
  end
end

new操作需要等效添加。 再一次,我确定必须有更好的方法,但是我是Rails的新手,所以这是我能做的。

这样可以很好地处理所有事情。 希望这对其他人有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM