Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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_Simple Form - Fatal编程技术网

Ruby on rails 为什么我的控制器在新建后调用更新而不是创建

Ruby on rails 为什么我的控制器在新建后调用更新而不是创建,ruby-on-rails,simple-form,Ruby On Rails,Simple Form,当我进入new\u heuristic\u variant\u cycle\u path时,我的应用程序会显示cycle的新视图,但表单上的提交按钮会显示“Update cycle”而不是“Create cycle”,当您单击提交按钮时,控制器会查找更新操作。为什么? 我有 /config/routes.rb: Testivate::Application.routes.draw do resources :heuristics do resources :variants do

当我进入
new\u heuristic\u variant\u cycle\u path
时,我的应用程序会显示cycle的新视图,但表单上的提交按钮会显示“Update cycle”而不是“Create cycle”,当您单击提交按钮时,控制器会查找更新操作。为什么?

我有

/config/routes.rb:

Testivate::Application.routes.draw do
  resources :heuristics do
    resources :variants do
      resources :cycles
    end
  end
end
class Heuristic < ActiveRecord::Base
  has_many :variants
  has_many :cycles, :through => :variants
end
class Variant < ActiveRecord::Base
  belongs_to :heuristic
  has_many :cycles
end
class Cycle < ActiveRecord::Base
  belongs_to :variant
end
%h1 New Cycle
= render 'form'
= simple_form_for [@heuristic, @variant, @cycle] do |f|
  = f.button :submit
class CyclesController < ApplicationController
  def new
    @heuristic = Heuristic.find(params[:heuristic_id])
    @variant = @heuristic.variants.find(params[:variant_id])
    @cycle = @variant.cycles.create
    respond_to do |format|
      format.html # new.html.erb
    end
  end
  def create
    @heuristic = Heuristic.find(params[:heuristic_id])
    @variant = @heuristic.variants.find(params[:variant_id])
    @cycle = @variant.cycles.create(params[:cycle])
    respond_to do |format|
      if @cycle.save
        format.html { redirect_to heuristic_variant_cycles_path(@heuristic, @variant, @cycle), notice: 'Cycle was successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
  end
end
/app/models/heuristics.rb:

Testivate::Application.routes.draw do
  resources :heuristics do
    resources :variants do
      resources :cycles
    end
  end
end
class Heuristic < ActiveRecord::Base
  has_many :variants
  has_many :cycles, :through => :variants
end
class Variant < ActiveRecord::Base
  belongs_to :heuristic
  has_many :cycles
end
class Cycle < ActiveRecord::Base
  belongs_to :variant
end
%h1 New Cycle
= render 'form'
= simple_form_for [@heuristic, @variant, @cycle] do |f|
  = f.button :submit
class CyclesController < ApplicationController
  def new
    @heuristic = Heuristic.find(params[:heuristic_id])
    @variant = @heuristic.variants.find(params[:variant_id])
    @cycle = @variant.cycles.create
    respond_to do |format|
      format.html # new.html.erb
    end
  end
  def create
    @heuristic = Heuristic.find(params[:heuristic_id])
    @variant = @heuristic.variants.find(params[:variant_id])
    @cycle = @variant.cycles.create(params[:cycle])
    respond_to do |format|
      if @cycle.save
        format.html { redirect_to heuristic_variant_cycles_path(@heuristic, @variant, @cycle), notice: 'Cycle was successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
  end
end
/app/views/cycles/_-form.html.haml:

Testivate::Application.routes.draw do
  resources :heuristics do
    resources :variants do
      resources :cycles
    end
  end
end
class Heuristic < ActiveRecord::Base
  has_many :variants
  has_many :cycles, :through => :variants
end
class Variant < ActiveRecord::Base
  belongs_to :heuristic
  has_many :cycles
end
class Cycle < ActiveRecord::Base
  belongs_to :variant
end
%h1 New Cycle
= render 'form'
= simple_form_for [@heuristic, @variant, @cycle] do |f|
  = f.button :submit
class CyclesController < ApplicationController
  def new
    @heuristic = Heuristic.find(params[:heuristic_id])
    @variant = @heuristic.variants.find(params[:variant_id])
    @cycle = @variant.cycles.create
    respond_to do |format|
      format.html # new.html.erb
    end
  end
  def create
    @heuristic = Heuristic.find(params[:heuristic_id])
    @variant = @heuristic.variants.find(params[:variant_id])
    @cycle = @variant.cycles.create(params[:cycle])
    respond_to do |format|
      if @cycle.save
        format.html { redirect_to heuristic_variant_cycles_path(@heuristic, @variant, @cycle), notice: 'Cycle was successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
  end
end
/app/controllers/cycles\u controller.rb:

Testivate::Application.routes.draw do
  resources :heuristics do
    resources :variants do
      resources :cycles
    end
  end
end
class Heuristic < ActiveRecord::Base
  has_many :variants
  has_many :cycles, :through => :variants
end
class Variant < ActiveRecord::Base
  belongs_to :heuristic
  has_many :cycles
end
class Cycle < ActiveRecord::Base
  belongs_to :variant
end
%h1 New Cycle
= render 'form'
= simple_form_for [@heuristic, @variant, @cycle] do |f|
  = f.button :submit
class CyclesController < ApplicationController
  def new
    @heuristic = Heuristic.find(params[:heuristic_id])
    @variant = @heuristic.variants.find(params[:variant_id])
    @cycle = @variant.cycles.create
    respond_to do |format|
      format.html # new.html.erb
    end
  end
  def create
    @heuristic = Heuristic.find(params[:heuristic_id])
    @variant = @heuristic.variants.find(params[:variant_id])
    @cycle = @variant.cycles.create(params[:cycle])
    respond_to do |format|
      if @cycle.save
        format.html { redirect_to heuristic_variant_cycles_path(@heuristic, @variant, @cycle), notice: 'Cycle was successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
  end
end
类循环控制器
在控制器中,这行代码错误:

@cycle = @variant.cycles.create
应该是这样的:

@cycle = @variant.cycles.build
调用
create
时,记录将被保存。在文件中:

collection.build(attributes={},…)

返回集合类型的一个或多个新对象,这些对象已使用属性实例化并通过外键链接到此对象,但尚未保存

create(attributes={})

返回集合类型的新对象,该对象已使用属性实例化,通过外键链接到此对象,并且已保存(如果已通过验证)。注意:这仅在数据库中已存在基本模型时有效,而不是在新记录(未保存)时有效