form for - rails form_for create new entry instead of updating -
i using form_for in order create new entry.
in controller set:
def create_ab_campaign @campaign = campaign.find_by(id: params[:id], account_id: current_account.id) @campaign_id = params[:id] end
in view do:
<%= form_for (@campaign), :url => {:controller => "campaigns", :action => "create_new_ab_campaign"} |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" %>
on submit want to create new entry instead of updating existing one.
i did following in controller:
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
i following: nomethoderror in campaignscontroller#update
it tries still use update instead of "create_a_new_ab_campaign"
i used update updating records - works fine.
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
but described instead of update/edit want case , create new entry out of record.
thanks lot help!
update:
full error log:
nomethoderror in campaignscontroller#update undefined method `update_attributes' nil:nilclass
extracted source (around line #124): 122 123 124 125 126 127
@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/sschindler/demostore
application trace | framework trace | full trace app/controllers/campaigns_controller.rb:124:in `update'
update routes
routes:
post "campaigns/create_new_ab_campaign"
get "campaigns/create_new_ab_campaign"
post "/campaigns/:id/edit" => "campaigns#edit"
get "/campaigns/:id/edit" => "campaigns#edit"
post "/campaigns/create_ab_campaign" => "campaigns#create_ab_campaign"
get "/campaigns/create_ab_campaign" => "campaigns#create_ab_campaign"
post "/campaigns/:id" => "campaigns#edit"
post "/campaigns/:id" => "campaigns#new"
get "/campaigns/:id" => "campaigns#edit"
get 'campaigns/show'
get 'campaigns/index'
post '/campaigns/edit'
get '/campaigns/edit'
i'm assuming create_ab_campaign
new route , create_ab_campaign_new
post route. although little confused because have params[:id]
value in #create_ab_campaign
from looks of though, form trying update because you're passing form_for
created campaign variable: @campaign = campaign.find_by(...)
rails form_for
check if object instance in db or if new model. if been created, patch
update route; if new model (but not created) post
create route.
in #new
route, need assign @campaign
campaign.new(account_id:current_account.id)
instead of finding 1 thats been made.
update
based on comments, want duplicate model, change of fields , save new record. in case, you'd want send id of campaign model want copy form, can retrieve in create route, use model.dup, modify params, save.
def create_ab_campaign @campaign = campaign.new @campaign_id = params[:id] end
add hidden field form
<%= form_for (@campaign), :url => {:controller => "campaigns", :action => "create_new_ab_campaign"} |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" %>
create route
dup campaign, make modifications, , check validity. sure not have uniqueness validations on model or youll run bit of trouble
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
Comments
Post a Comment