我使用form_for来创建一个新条目。
在我设置的控制器中:
def create_ab_campaign
@campaign = Campaign.find_by(id: params[:id], account_id: current_account.id)
@campaign_id = params[:id]
end
在我看来:
<%= form_for (@campaign), :url => {:controller => "campaigns", :action => "create_new_ab_campaign"} do |f| %>
<div class="form-group">
<%= f.label :name, "name", class: "control-label" %>
<%= f.text_field :name, class: "form-control", placeholder: "name" %>
</div>
<%= f.submit "Save Settings" %>
提交时,我希望创建一个新条目,而不是更新现有条目。
我在控制器中做了以下操作:
def create_ab_campaign_new
@campaign_new = Campaign.new(create_a_new_ab_campaign)
if @campaign_new.save
redirect_to(:controller =>"campaigns", :action =>"index")
else
redirect_to(:action =>"new")
end
end
private
def create_a_new_ab_campaign
params.require(:campaign).permit(:name)
end
我得到以下内容:CampaignsController#更新中的NoMethodError
它仍然尝试使用更新,而不是"create_a_new_ab_campaign">
我使用更新来更新记录-这一切都很好。
def update
@campaign = Campaign.find_by(id:params[:id], account_id: current_account.id)
@new = create_new_campaign
if @campaign.update_attributes(@new)
redirect_to(:controller =>"campaigns", :action =>"index")
else
render 'edit'
end
end
但正如所描述的,我想做另一个案例,从记录中创建一个新条目,而不是更新/编辑。
非常感谢你的帮助!
更新:
完整错误日志:
CampaignsController#更新中的NoMethodErrornil:NilClass 的未定义方法"update_attributes">
提取来源(124号线附近(:122123124125126127
@new = create_new_campaign
@new[:page_type].delete_if{ |x| x.empty? }
if @campaign.update_attributes(@new)
redirect_to(:controller =>"campaigns", :action =>"index")
else
render 'edit'
Rails.root:/Users/schindler/demostore
应用程序跟踪|框架跟踪|完全跟踪app/controllers/campaigns_controller.rb:124:在"更新"中
更新路线
路线:
张贴"活动/create_new_ab_campaign">
获取"活动/create_new_ab_campaign">
post"/acampaigns/:id/edit"=>"campaigns#edit">
get"/acampaigns/:id/edit"=>"campaigns#edit">
post"/create_ab_campaign"=>"campaign#create_ab_camaign">
get"/create_ab_campaign"=>"campaign#create_ab_campign">
post"/活动/:id"=>"活动#edit">
post"/活动/:id"=>"活动#new">
get"/活动/:id"=>"活动#edit">
获取"活动/展示">
获取"活动/索引">
张贴"/活动/编辑">
获取"/活动/编辑">
我假设您的create_ab_campaign
是您的新路线,create_ab_campaign_new
是您的后路线。虽然我有点困惑,因为你在#create_ab_campaign
中有一个params[:id]
值
不过,从外观上看,您的表单正在尝试更新,因为您正在向form_for
传递一个已经创建的活动变量:@campaign = Campaign.find_by(...)
rails form_for
将检查对象是否是数据库中的实例,或者它是否只是一个新模型。如果它已经创建,它将PATCH
发送到您的更新路由;如果它是一个新模型(但尚未创建(,它将POST
发送到您的创建路线。
在您的#new
路由中,您需要将@campaign
分配给Campaign.new(account_id:current_account.id)
,而不是找到一个已经制作好的。
更新
根据您的评论,您希望复制一个模型,更改一些字段并将其另存为新记录。在这种情况下,您需要将要复制的活动模型的id发送到表单中,这样您就可以在创建路线中检索它,使用model.dup,用params修改它,然后保存。
def create_ab_campaign
@campaign = Campaign.new
@campaign_id = params[:id]
end
添加隐藏字段以形成
<%= form_for (@campaign), :url => {:controller => "campaigns", :action => "create_new_ab_campaign"} do |f| %>
<div class="form-group">
<%= hidden_field_tag :campaign_id, @campaign_id
<%= f.label :name, "name", class: "control-label" %>
<%= f.text_field :name, class: "form-control", placeholder: "name" %>
</div>
<%= f.submit "Save Settings" %>
创建路线
重复活动,进行修改,并检查有效性。请确保不要对此模型进行唯一性验证,否则会遇到一些麻烦
def create_ab_campaign_new
@campaign_new = Campaign.find(params[:campaign_id]).dup
@campaign_new.update(create_a_new_ab_campaign)
if @campaign_new.save
redirect_to(:controller =>"campaigns", :action =>"index")
else
redirect_to(:action =>"new")
end
end
看起来您正在混合VERBS
`post "campaigns/create_new_ab_campaign"`
是邮政和
= form_for (@campaign), :url => {:controller => "campaigns", :action => "create_new_ab_campaign"} do |f|
是PUT。尝试更改为
在路线上。rb:
match 'create_new_ab_campaign', to 'campaigns#create_new_ab_campaign', via: :post
然后
=form_for(@campaign), url: :create_new_ab_campaign, method: 'POST' do |f|
基本上如果对新对象调用form_for,则表单提交方法默认为POST
;如果对现有对象使用,则表单发送方法默认为PUT/PATCH
。这就是为什么它一直在寻找更新。要更改此设置,请将method
关键字添加到表单生成器中。
进一步阅读:http://www.restapitutorial.com/lessons/httpmethods.html