Rails 4,如何从不同的控制器更新模型字段?

时间:2015-10-10 02:18:32

标签: ruby-on-rails

我正在尝试更新发票字段,在购物车控制器中结帐时。这些必须在结账时出现,否则应该失败。但是,我无法更新它,更不用说验证它们了。 这是我的代码:

购物车展示视图:

<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 do |f| %>
            <%= submit_tag 'Complete', class: 'btn btn-success' %>
        <% end %>
    </div>
</div>

推车控制器:

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是当前会话的发票,与购物车相关。它重定向正确,但不会更新。

发票控制器中的

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

任何人都可以帮我确定我哪里错了吗?可能是我的方法甚至没有效果吗?

提前致谢

2 个答案:

答案 0 :(得分:1)

您所使用的功能类型被视为“业务逻辑”,应在模型中实现并从控制器调用。

您可以在模型中定义方法:

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 to update record. Handle the error."
  end
end

您可以这样从my_method致电carts_controller.rb

def update
  # all your regular update logic here

  # replace the bit of code that saves the cart with something like this:

  respond_to do |format|
    if(current_invoice.update_invoice(params[:customer_id], params[:employee_id])
      if(@cart.update(cart_params))
        format.html { redirect_to @activity, notice: 'Activity was successfully 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

另请注意使用update_attributes而不是save。请记住,如果您遇到任何更新问题(例如,一个或多个验证失败),update_attributes将返回false。不要将update_attributes与更新单个字段的单数update_attribute混淆,并且运行验证。

答案 1 :(得分:0)

终于明白了。

current_invoice.update_attributes(customer_id: params[:invoice][:customer_id], employee_id: params[:invoice][:employee_id])

同样在视图中,更改了form_tag的位置:

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