我开发了一个Rails 3.1应用程序,它由100%的Ajax CRUD表单组成,用于输入用户和客户.我创建了两个独立的模型,控制器,视图等.每个模型的视图包含用于创建编辑更新和销毁的jquery文件.问题是当我执行任何执行远程操作的操作时,它被调用两次.我可以在firebug的控制台视图中确认这种情况以及WEBrick输出中的输出.任何人都可以帮助追踪这里发生的事情吗?什么会导致rails处理每次调用两次?
我有一个客户端模型和一个产品模型,其中客户端有许多产品,而产品属于CLient.
我需要找到一个只返回客户端的查询,如果他们在Product表中有记录
客户表
id | name
--------------
1 | Company A
2 | Company B
3 | Company C
Run Code Online (Sandbox Code Playgroud)
产品表
id | name | client_id
---------------------------
1 | Product A | 1
2 | Product B | 1
3 | Product C | 3
4 | Product D | 3
5 | Product E | 1
Run Code Online (Sandbox Code Playgroud)
我只需要客户1 3
比如像
@clients = Client.where("client exists in products") #something to this effect
Run Code Online (Sandbox Code Playgroud) 我正在尝试为具有嵌套属性的模型(FormArray)实现编辑表单.我的语法有问题,我不确定自己是否走在正确的轨道上.主表单的属性工作,它是我遇到问题的嵌套表单.这是我到目前为止所拥有的.
在这里,我启动表单组:
private initForm() {
this.subscription = this.expenseService.getExpense(this.id)
.subscribe(
expense => {
this.expense = expense;
this.patchForm();
}
);
this.expenseEditForm = this.fb.group({
date: '',
amount: '',
check_number: '',
debit: '',
payee_id: '',
notes: '',
expense_expense_categories_attributes:[]
});
}
Run Code Online (Sandbox Code Playgroud)
在这里,我修补表单以设置从我的后端API检索的对象的值.
private patchForm() {
this.expenseEditForm.setValue({
date: '',
amount: this.expense.amount_cents,
check_number: this.expense.check_number,
debit: this.expense.debit,
payee_id: '',
notes: this.expense.notes,
expense_expense_categories_attributes: this.fb.array([
this.setExpenseCategories(),
])
});
}
Run Code Online (Sandbox Code Playgroud)
这就是我被困住的地方.如何推送FormArray.如果我尝试推送,我会收到一个错误,指出它在FormArray上不存在.
private setExpenseCategories() {
for ( let expenseCategories of this.expense.expense_expense_categories){
this.fb.array.push([
this.fb.group({
expense_category_id: [expenseCategories.expense_category_id, Validators.required],
amount: [expenseCategories.amount_cents]
])
});
}
} …Run Code Online (Sandbox Code Playgroud) 我正在使用Rails 4.0开发一个应用程序,我遇到的问题是turbolinks与我的一些jQuery代码不兼容.我有一个Quote模型,它有一个相关的QuoteItems模型.我使用accepts_nested_attributes_for和一些jQuery来填充订单项表单.
当我点击链接将我带到new_quote_path时,动态链接不会触发javascript代码.当我刷新页面时,表单工作很棒.我喜欢turbolinks,因为它超级快,但不知道如何让它在开发中工作.这是一些代码.
在quotes.js.coffee中
jQuery ->
$('form').on 'click', '.remove_line_items', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('fieldset').hide()
event.preventDefault()
$('form').on 'click', '.add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
Run Code Online (Sandbox Code Playgroud)
报价查看new.html.erb
<%= form_for @quote, :class => "hello" do |f| %>
<fieldset>
<p>
<%= f.label :quote_date, "Date of Quote" %> <br/>
<%= f.text_field :quote_date %>
</p>
<p>
<%= f.label :good_through %> <br/>
<%= f.text_field :good_through %>
</p>
<p>
<%= f.label :quote_number %><br/>
<%= f.text_field :quote_number %>
</p>
<p>
<%= f.label …Run Code Online (Sandbox Code Playgroud) 我有一个在开发模式下工作得很漂亮的应用程序.为了尝试在Webrick或Passenger/apache中进行测试,我的大多数网站都会加载,直到我尝试提交ajax表单.我已经正确使用了bundle install --deployment.我已正确预编译了我的资产.但由于某种原因,我在提交远程表单时遇到以下错误.请记住,ajax实际上正在工作,因为它在数据库中创建记录.我觉得有趣的一件事是我使用带有ruby 1.9.3的gemset但我在这些错误代码中得到了对ruby 1.9.1的引用.我还包括用户控制器,以便您可以看到控制器参考.救命!
更新!! 根据创建操作的动作,编辑操作更新操作或销毁错误缺少模板用户/创建或用户/更新或用户/编辑用户/销毁等.阅读第一个答案的评论,因为我认为这是一个jcomp文件未包含在预编译过程中的问题.
Started GET "/users/5" for 24.163.20.124 at 2012-02-07 03:30:09 -0500
Processing by UsersController#show as HTML
Parameters: {"id"=>"5"}
Completed 500 Internal Server Error in 58ms
ActionView::MissingTemplate (Missing template users/show, application/show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder]}. Searched in:
* "/home/ctilley/Development/RatatouilleCatering/app/views"
* "/home/ctilley/Development/RatatouilleCatering/vendor/bundle/ruby/1.9.1/gems/wash_out-0.3.1/app/views"
* "/home/ctilley/Development/RatatouilleCatering/vendor/bundle/ruby/1.9.1/gems/ckeditor-3.7.0.rc2/app/views"
):
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/path_set.rb:58:in `find'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/lookup_context.rb:109:in `find'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/renderer/abstract_renderer.rb:3:in `find_template'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/renderer/template_renderer.rb:28:in `determine_template'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/renderer/template_renderer.rb:10:in `render'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/renderer/renderer.rb:36:in `render_template'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_view/renderer/renderer.rb:17:in `render'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/abstract_controller/rendering.rb:109:in `_render_template'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_controller/metal/streaming.rb:225:in `_render_template'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/abstract_controller/rendering.rb:103:in `render_to_body'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_controller/metal/renderers.rb:28:in `render_to_body'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/abstract_controller/rendering.rb:88:in `render'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_controller/metal/rendering.rb:16:in `render'
vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.0/lib/action_controller/metal/instrumentation.rb:40:in `block (2 …Run Code Online (Sandbox Code Playgroud) production-environment ruby-on-rails-3.1 asset-pipeline ruby-on-rails-3.2
我有一个动态生成的表,乘以价格*数量.部分价格为部分美分.例如
如果一个项目的价格是0.0375我可以在我的表中显示为
number_to_currency(0.0375,:precision => 4)
=> $0.0375
Run Code Online (Sandbox Code Playgroud)
但是对于价格是标准的2位十进制数的数量,我得到
number_to_currency(33.95,:precision => 4)
=> $39.9500
Run Code Online (Sandbox Code Playgroud)
我需要一种方法来修剪十进制值的尾随零.请记住,输出在Model.each块中,所以我不确定我是否可以有条件地修改precision参数.
我能够将表数据导出到CSV文件,但每条记录后都有一个空行.为什么以及如何解决它?
在index.html.erb中
<%= link_to "Export to csv", request.parameters.merge({:format => :csv})%>
Run Code Online (Sandbox Code Playgroud)
在index.csv.erb中
<%- headers = ["Id", "Name"] -%>
<%= CSV.generate_line headers %>
<%- @customers.each do |n| -%>
<%- row = [ n.id, n.fname ] -%>
<%= CSV.generate_line row %>
<%- end -%>
Run Code Online (Sandbox Code Playgroud) 好.我正在删除public/assets文件夹中的预编译资产.我不小心删除了我在那里的整个文件夹.我检查了一个存储库并开始webrick并且文件似乎正在加载,但是当我查看目录时它们不在那里?如何将这些文件恢复/移回公共/资产目录?
我有以下范围来查找属于特定客户的产品.
scope :client, lambda {|client| where("client_id = ?", client) }
Run Code Online (Sandbox Code Playgroud)
并且可以被称为
Product.client(parameter)
Run Code Online (Sandbox Code Playgroud)
如果没有给出客户ID,我有什么方法可以声明我的范围返回所有产品?这是不应该使用范围的情况吗?
我有一个看起来像这样的数组:
@shipment_products
[
{"old_qty_shipped"=>"324", "product_id"=>"1", "qty_shipped"=>"12443"}
{"old_qty_shipped"=>"4343423", "product_id"=>"3", "qty_shipped"=>"321344"}
{"old_qty_shipped"=>"23", "product_id"=>"4", "qty_shipped"=>"321"}
]
Run Code Online (Sandbox Code Playgroud)
我想最终能够做到这样的事情
@shipment_products.each do |p|
Product.adjust_qtys(p.old_qty_shipped, p.qty_shipped, p.product_id)
end
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
NoMethodError (undefined method `qty_shipped' for #<ActiveSupport::HashWithIndifferentAccess:0x007f>)
Run Code Online (Sandbox Code Playgroud)
这个数组的格式不是很合适.我需要找到一种方法来迭代键/值并提取属性,以便我可以调用我在模型中创建的方法.有任何想法吗?
jquery ×2
ruby ×2
activerecord ×1
angular ×1
arrays ×1
crud ×1
csv ×1
git ×1
hash ×1
iteration ×1
turbolinks ×1
typescript ×1