在rails中处理多个到多个帮助器视图的正确方法是什么?

时间:2011-10-28 22:04:44

标签: ruby-on-rails ruby-on-rails-3

我需要在创建/更新表单中的每个字段旁边添加一个选择框,以确定其出处。有一张桌子上有摄影师信息,一张有源头的桌子,还有一张表,它们之间建立了多对多的关系。关系将取得摄影师的身份,该记录中的列(例如,姓氏的“最后”)以及该信息来源的来源的ID(表格中的数据来自不同的来源,非常精细) - 成熟的方式)。

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

我正在使用带有mysql2连接器的Ruby on Rails 3.0.3。

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

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

但我不知道如何:

  1. 预先选择id与多对多表的source_id
  2. 匹配的来源
  3. 将该数据发回(columnsource_id)并在数据库中执行关联
  4. 我的架构:

    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
    

1 个答案:

答案 0 :(得分:0)

我在源代码的选择框中添加了一个部分表单:

<%
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'}) %>

您可以将“类别”更改为字段名称。我相信一定有更好的方法,但这对我来说已经足够了。

请注意,select中生成的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的新手,所以这是我能够做到的。

这很好地照顾了一切。希望这有助于其他人。