Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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 创建多个:通过记录时跳过验证_Ruby On Rails_Ruby On Rails 3_Validation_Nested Forms - Fatal编程技术网

Ruby on rails 创建多个:通过记录时跳过验证

Ruby on rails 创建多个:通过记录时跳过验证,ruby-on-rails,ruby-on-rails-3,validation,nested-forms,Ruby On Rails,Ruby On Rails 3,Validation,Nested Forms,在my db中,有很多用户的数据无效(没有手机号码)。因为我刚才添加了移动状态验证。当我尝试添加新事件时,会收到错误消息“Users mobile number必须为…格式”。创建事件时如何跳过用户验证 event.rb class Event < ActiveRecord::Base has_many :participations has_many :users, :through => :participations end class User < Activ

在my db中,有很多用户的数据无效(没有手机号码)。因为我刚才添加了移动状态验证。当我尝试添加新事件时,会收到错误消息“Users mobile number必须为…格式”。创建事件时如何跳过用户验证

event.rb

class Event < ActiveRecord::Base
  has_many :participations
  has_many :users, :through => :participations
end
class User < ActiveRecord::Base
  has_many :participations
  has_many :events, :through => :participations
end
 def create
    @event = Event.new(event_params)

    respond_to do |format|
      if @event.save
        format.html { redirect_to events_path, notice: 'Event was successfully created.' }
      else
        format.html { render action: 'new' }
      end
    end
  end
 <%= form_for @event, :html => {:class => 'row'} do |f| %>
      <%= f.error_messages %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :user_ids, "Participants" %>
      <%= f.collection_select :user_ids, User.all, :id, :name, {}, { :multiple => true } %>

      <%= clear %>

      <%= f.submit 'Save' %>
    <% end %>
\u form.html.erb

class Event < ActiveRecord::Base
  has_many :participations
  has_many :users, :through => :participations
end
class User < ActiveRecord::Base
  has_many :participations
  has_many :events, :through => :participations
end
 def create
    @event = Event.new(event_params)

    respond_to do |format|
      if @event.save
        format.html { redirect_to events_path, notice: 'Event was successfully created.' }
      else
        format.html { render action: 'new' }
      end
    end
  end
 <%= form_for @event, :html => {:class => 'row'} do |f| %>
      <%= f.error_messages %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :user_ids, "Participants" %>
      <%= f.collection_select :user_ids, User.all, :id, :name, {}, { :multiple => true } %>

      <%= clear %>

      <%= f.submit 'Save' %>
    <% end %>
{:class=>'row'}do | f |%>
真}%>

Set
validate:false
在事件
有多个:用户关联时

class Event < ActiveRecord::Base
  has_many :participations
  has_many :users, :through => :participations, validate: false
end
class事件:participations,验证:false
结束

我想知道如何在Rails 5.0.0.RC1中实现这一点。谢谢。创建父记录并分配子id(如
Event.create(用户id:[1,2,3])
Rails在每个用户上运行
valid?
,而不分配
Event\u id
)。因此,您无法检测事件id是否已更改并跳过验证:(.非常感谢,这个答案对我非常有帮助。