Rails 4, how to update a model field from a different controller? -


i trying update invoice fields, when checking out in carts controller. these must present when checking out, or should fail. however, can't update, less validate them. here code:

cart show view:

<div class = "row">     <div class = "col-lg-3 col-lg-offset-6 text-left">         <strong>customer: </strong>         <%= collection_select(:invoice, :customer_id, @customers, :id, :full_name, {:prompt => 'please select'}, class: 'form-control') %>     </div>     <div class = "col-lg-3 ext-left">         <strong>seller: </strong>         <%= collection_select(:invoice, :employee_id, @employees, :id, :full_name, {:prompt => 'please select'}, class: 'form-control') %>     </div>     <div class = "col-lg-12 text-right">         <%= form_tag carts_checkout_path, method: :post |f| %>             <%= submit_tag 'complete', class: 'btn btn-success' %>         <% end %>     </div> </div> 

carts controller:

class cartscontroller < applicationcontroller   def show     @invoice = current_invoice     @invoice_products = current_invoice.invoice_products     @customers = customer.all     @employees = employee.all   end   def checkout      current_invoice.customer_id = params[:customer_id]     current_invoice.employee_id = params[:employee_id]     current_invoice.save     redirect_to current_invoice   end end 

current_invoice current session's invoice, related cart. redirects correctly, doesn't update.

in invoices controller:

def invoice_params   params.require(:invoice).permit(:invoice_number, :customer_id, :invoice_date, :invoice_status_id, :employee_id, invoice_products_attributes: [:id, :invoice_id, :product_id, :price, :tax, :discount, :value]) end 

can please me in identifying going wrong? approach not valid?

thanks in advance

the type of functionality you're after considered "business logic" , should implemented in model , called controller.

you can define method in model:

class invoice < activerecord::base    def update_invoice(cust_id, emp_id)     if self.update_attributes(:customer_id => cust_id], :employee_id = emp_id])       puts "success!     else      puts "failed update record. handle error."   end end 

you can call my_method carts_controller.rb this:

def update   # regular update logic here    # replace bit of code saves cart this:    respond_to |format|     if(current_invoice.update_invoice(params[:customer_id], params[:employee_id])       if(@cart.update(cart_params))         format.html { redirect_to @activity, notice: 'activity updated.' }         format.json { render :show, status: :ok, location: @activity }       else         format.html { render :edit }         format.json { render json: @activity.errors, status: :unprocessable_entity }       end     end   end 

also, note use of update_attributes rather save. bear in mind update_attributes return false if run problems updating (e.g. 1 or more validations failed). don't confuse update_attributes singular update_attribute updates single field , not run validations.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -