Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么这个选择字段在MyRails表单中多次出现?_Ruby On Rails_Forms_Rails Activerecord_Ruby On Rails 2 - Fatal编程技术网

Ruby on rails 为什么这个选择字段在MyRails表单中多次出现?

Ruby on rails 为什么这个选择字段在MyRails表单中多次出现?,ruby-on-rails,forms,rails-activerecord,ruby-on-rails-2,Ruby On Rails,Forms,Rails Activerecord,Ruby On Rails 2,我有一个使用嵌套表单的Rails应用程序。详细信息如下,我尝试了这个解决方案(),但由于某种原因,该字段重复了多次,而没有正确列出和保存选项。提前感谢您的帮助 <% f.fields_for :waypoints do |w| %> <%= w.collection_select( :waypointaddress, @newsavedmap.waypoints, :waypointaddress, :waypointaddress, {:includ

我有一个使用嵌套表单的Rails应用程序。详细信息如下,我尝试了这个解决方案(),但由于某种原因,该字段重复了多次,而没有正确列出和保存选项。提前感谢您的帮助

    <% f.fields_for :waypoints do |w| %> 
        <%= w.collection_select( :waypointaddress, @newsavedmap.waypoints, :waypointaddress, :waypointaddress, {:include_blank => true}, {:id =>"waypoints"} ) %>
        <% end %>
Newsavedmaps的型号信息

has_many :waypoints, :dependent => :destroy
accepts_nested_attributes_for :waypoints
Newsavedmap\u控制器

def new
  @newsavedmap = Newsavedmap.new
  waypoint = @newsavedmap.waypoints.build
  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @newsavedmap }
  end
end

def edit
  @newsavedmap = Newsavedmap.find(params[:id])
  if @newsavedmap.itinerary.user_id == current_user.id
    respond_to do |format|
      format.html # edit.html.erb
      format.xml  { render :xml => @activity }
    end  
  else
    redirect_to '/'
  end
end
地图视图

<% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
  <%= f.error_messages %>
  <% f.fields_for :waypoint do |w| %>
    <%= w.select :waypointaddress, options_for_select(Waypoint.find(:all, :conditions => {:newsavedmap_id => params[:newsavedmap_id]}).collect {|wp| [wp.waypointaddress, wp.waypointaddress] }), {:include_blank => true}, {:multiple => true, :class => "mobile-waypoints-remove", :id =>"waypoints"} %>
  <% end %>
<% end %>
当我在视图中将“:waypoint do | w |”更改为“:waypoints do | w |”时,当用户创建新记录时,“选择”字段将消失,并且在“编辑”视图中,“选择”字段会出现几次(无论用户在记录中保存了多少个航路点)

如何使此表单字段正常工作

编辑1

这是我的最新尝试。对于新记录,选择字段不会出现。但是,在“编辑”视图中,“选择”字段会出现多次。这是一个Rails 2应用程序,仅供参考。根据评论的提示,我使用了收集选择的方法(不是收集选择,因为我找不到相关文档。)再次感谢您的帮助

    <% f.fields_for :waypoints do |w| %> 
        <%= w.collection_select( :waypointaddress, @newsavedmap.waypoints, :waypointaddress, :waypointaddress, {:include_blank => true}, {:id =>"waypoints"} ) %>
        <% end %>

true},{:id=>“航路点”})%>

您的表单存在以下问题

  • f.fields\u用于:航路点
    ,因为参数需要与关联的名称匹配
  • 使用而不是选择,因为该字段后面有一个ActiveRecord模型
  • 因此,考虑到这一点,您可以在表单中尝试:

    <% form_for @newsavedmap, :html => { :id => 'createaMap' } do |f| %>
      <%= f.error_messages %>
      <% f.fields_for :waypoints do |w| %>
        <%= w.collection_for_select :waypoint, :waypointaddress, @newsavedmap.waypoints, :waypointaddress, :waypointaddress, { :include_blank => true }, { :multiple => true, :class => "mobile-waypoints-remove", :id =>"waypoints" } %>
      <% end %>
    <% end %>
    
    {:id=>'createaMap'}do | f |%>
    true},{:multiple=>true,:class=>“移动航路点删除”,:id=>“航路点”}%>
    

    collection\u select
    的API有点棘手。我通常也需要几次尝试。前一个SO问题可能有助于澄清问题:

    第一件事优先。如果关联名称为
    航路点
    ,则必须使用
    f.fields\u:waypoints do | w |
    。当然,您没有名为
    waypoint
    的属性,因为这不是关联的名称。那么“选择字段消失”的确切含义是什么?这听起来不像Rails的问题。谢谢,depa,但是当我这样做时,当用户创建新记录时,选择字段会消失,并且在编辑视图中,选择字段会出现几次(无论用户在记录中保存了多少个航路点)。By消失,我的意思是,选择字段不会显示在新记录的源代码或浏览器显示中。但是,它会在“编辑”视图中多次出现。再次感谢你的帮助!是的,我读过了,如果你再重复一遍也没用,因为第一次读起来对我没有帮助。您是否使用JavaScript创建新的选择或类似内容?另外,由于您在“编辑”视图中遇到问题,请在控制器中发布编辑操作的内容。感谢您的持续帮助。不幸的是,当我尝试上面的代码时,当用户创建新记录或编辑记录时,选择字段不会出现。