更新操作触发插入查询?
嗨,我知道这很愚蠢,但确实,当我编辑任何条目时,我得到了这个。
我有一个费用模型、一个expense_line_item 和一个paid_line_item 模型,当我创建新条目时它会正确创建,但在编辑上一个条目时它会添加一个新条目而不是更新,即在更新操作时它会触发插入查询。这是我的代码:
我的控制器:
def new
@menu = 'Expenses'
@page_name = 'Record New Expenses'
@expense = Expense.new
@expense.expense_line_items.build
@expense.paid_line_items.build
@expense.voucher_number = "EXP"+Time.now.to_i.to_s
@from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
@to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments')
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @expense }
end
end
# GET /expenses/1/edit
def edit
@menu = 'Expenses'
@page_name = 'Edit Expenses Entry'
@expense = Expense.find(params[:id])
@from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
@to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments')
end
# POST /expenses
# POST /expenses.xml
def create
@expense = Expense.new(params[:expense])
@expense.created_by = current_user.id
@expense.company_id = current_company.id
respond_to do |format|
if @expense.save
format.html { redirect_to(@expense, :notice => 'Expense was successfully created.') }
format.xml { render :xml => @expense, :status => :created, :location => @expense }
else
@menu = 'Expenses'
@page_name = 'Record New Expenses'
@from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
@to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments')
format.html { render :action => "new" }
format.xml { render :xml => @expense.errors, :status => :unprocessable_entity }
end
end
end
# PUT /expenses/1
# PUT /expenses/1.xml
def update
@expense = Expense.find(params[:id])
respond_to do |format|
if @expense.update_attributes(params[:expense])
format.html { redirect_to(@expense, :notice => 'Expense was successfully updated.') }
format.xml { head :ok }
else
@menu = 'Expenses'
@page_name = 'Edit Expenses Entry'
format.html { render :action => "edit" }
format.xml { render :xml => @expense.errors, :status => :unprocessable_entity }
end
end
end
我的模型:
expense model:
class Expense < ActiveRecord::Base
has_many :expense_line_items
has_many :paid_line_items
accepts_nested_attributes_for :expense_line_items, :reject_if => lambda {|a| a[:account_id].blank? } , :allow_destroy => true
accepts_nested_attributes_for :paid_line_items, :reject_if => lambda {|a| a[:account_id].blank? }, :allow_destroy => true
#validations
validates_presence_of :expense_date, :voucher_number
validates_presence_of :expense_line_items
validates_associated :expense_line_items
validates_presence_of :paid_line_items
validates_associated :paid_line_items
end
expense_line_item:
class ExpenseLineItem < ActiveRecord::Base
belongs_to :expense
end
paid_line_item:
class PaidLineItem < ActiveRecord::Base
belongs_to :expense
end
我的表单:
<%= form_for(@expense) do |f| %>
<% @expense.expense_line_items.each_with_index do |expense_line_item, index| %>
<%= render "expense_line_items", :expense_line_item => expense_line_item, :index => index %>
<% end %>
<tr id="row_link" valign="top">
<td valign="top" colspan="6">
<%= link_to "Add new row",{:action => :add_row, :index => @expense.expense_line_items.size}, :remote => true %>
</td>
</tr>
<% @expense.paid_line_items.each_with_index do |paid_line_item, index| %>
<%= render "paid_line_items", :paid_line_item => paid_line_item, :index => index %>
<% end %>
<tr id="to_row_link" valign="top">
<td valign="top" colspan="6">
<%= link_to "Add new row",{:action => :add_to_row, :index => @expense.paid_line_items.size}, :remote => true %>
</td>
</tr>
<% end %>
我很沮丧,提前致谢。
Hi i know it's silly but true i got this when i edit any entry.
I have an expense model and an expense_line_item and a paid_line_item model, it is created properly when i create a new entry but on editing previous entry it adds a new entry instead of updating, i. e. at update action it fires insert query. here is my code:
My controller:
def new
@menu = 'Expenses'
@page_name = 'Record New Expenses'
@expense = Expense.new
@expense.expense_line_items.build
@expense.paid_line_items.build
@expense.voucher_number = "EXP"+Time.now.to_i.to_s
@from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
@to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments')
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @expense }
end
end
# GET /expenses/1/edit
def edit
@menu = 'Expenses'
@page_name = 'Edit Expenses Entry'
@expense = Expense.find(params[:id])
@from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
@to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments')
end
# POST /expenses
# POST /expenses.xml
def create
@expense = Expense.new(params[:expense])
@expense.created_by = current_user.id
@expense.company_id = current_company.id
respond_to do |format|
if @expense.save
format.html { redirect_to(@expense, :notice => 'Expense was successfully created.') }
format.xml { render :xml => @expense, :status => :created, :location => @expense }
else
@menu = 'Expenses'
@page_name = 'Record New Expenses'
@from_accounts = TransactionType.fetch_from_accounts(current_company.id, 'payments')
@to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'payments')
format.html { render :action => "new" }
format.xml { render :xml => @expense.errors, :status => :unprocessable_entity }
end
end
end
# PUT /expenses/1
# PUT /expenses/1.xml
def update
@expense = Expense.find(params[:id])
respond_to do |format|
if @expense.update_attributes(params[:expense])
format.html { redirect_to(@expense, :notice => 'Expense was successfully updated.') }
format.xml { head :ok }
else
@menu = 'Expenses'
@page_name = 'Edit Expenses Entry'
format.html { render :action => "edit" }
format.xml { render :xml => @expense.errors, :status => :unprocessable_entity }
end
end
end
My Model:
expense model:
class Expense < ActiveRecord::Base
has_many :expense_line_items
has_many :paid_line_items
accepts_nested_attributes_for :expense_line_items, :reject_if => lambda {|a| a[:account_id].blank? } , :allow_destroy => true
accepts_nested_attributes_for :paid_line_items, :reject_if => lambda {|a| a[:account_id].blank? }, :allow_destroy => true
#validations
validates_presence_of :expense_date, :voucher_number
validates_presence_of :expense_line_items
validates_associated :expense_line_items
validates_presence_of :paid_line_items
validates_associated :paid_line_items
end
expense_line_item:
class ExpenseLineItem < ActiveRecord::Base
belongs_to :expense
end
paid_line_item:
class PaidLineItem < ActiveRecord::Base
belongs_to :expense
end
My form :
<%= form_for(@expense) do |f| %>
<% @expense.expense_line_items.each_with_index do |expense_line_item, index| %>
<%= render "expense_line_items", :expense_line_item => expense_line_item, :index => index %>
<% end %>
<tr id="row_link" valign="top">
<td valign="top" colspan="6">
<%= link_to "Add new row",{:action => :add_row, :index => @expense.expense_line_items.size}, :remote => true %>
</td>
</tr>
<% @expense.paid_line_items.each_with_index do |paid_line_item, index| %>
<%= render "paid_line_items", :paid_line_item => paid_line_item, :index => index %>
<% end %>
<tr id="to_row_link" valign="top">
<td valign="top" colspan="6">
<%= link_to "Add new row",{:action => :add_to_row, :index => @expense.paid_line_items.size}, :remote => true %>
</td>
</tr>
<% end %>
i got frustrated, thanks in advance .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经找到了这个问题的解决方案。当我尝试更新一个 lineitem 时,它会将其视为新的 lineitem,因此我必须传递一个隐藏的 lineitem_id 来进行更新操作。我用过
下面的代码:
它对我有用。
i have found a solution for this problem. When i tried to update a lineitem it take it as a new one hence i have to pass a hidden lineitem_id for update action. i have used
below code :
and it works for me.