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