ruby on rails - Rails4 Entering data from one controller to multiple models -
i have of unique problem. have 3 tables in database need populate data. tables in relation each other. first table's info static , populated hash. second table table targeted data.
i having tough time trying add data second table using strong parameters. error param missing or value empty: entries
modles:
client.rb
class client < activerecord::base has_many :entries end entry.rb
class entry < activerecord::base belongs_to :client_name has_many :extra_data end extra_data.rb
class extradata < activerecord::base belongs_to :entries end class clientscontroller < applicationcontroller before_action :set_client, only: [:show, :update, :destroy, :edit] # submit intended purposes. # def new @entries = entry.new() end def create @client = client.new(cleint_attr) if @client.save @entries = entry.new(submit_params) redirect_to action: :index else flash.alert "you failed @ life today." redirect_to action: :index end end . . . private def submit_params params.require(:entries).permit( :full_name,:email,:opt_in ) end def set_client @client = client.find(params[:id]) end end form
<%= simple_form_for(:client, url: {:controller => 'clients', :action => 'create'}) |f| %> <%= f.input :full_name %> <%= f.input :email %> <%= f.input :opt_in %> <%= f.button :submit, class: "btn-primary" %> <% end %> routes:
rails.application.routes.draw resources :clients resources :entries resources :extra_data end end root 'clients#index' end in database client data goes in out problem. having problem getting data form itself.
this answer culmination of few different parts.
i figured out not saving data model. needed make if statement.
def create @client = client.new(cleint_attr) if @client.save @entries = entry.new(submit_params) if @entries.save flash[:alert] = "failure! working." redirect_to action: :index else flash[:alert] = "success! @ failing." end else flash[:alert] = "you failed @ life today." redirect_to action: :thanks end end also changing form :entries helped. had typo in permit statment. had :opt_in when needed use :optin @tmc
Comments
Post a Comment