是否有可能使用delegate与has_many或has_one在一个多态模型关联?这是如何运作的?
class Generic < ActiveRecord::Base
...
belongs_to :generable, polymorphic: true
delegate :file_url, to: :image, allow_nil: true
delegate :type_cat, to: :cat, allow_nil: true
end
class Image < ActiveRecord::Base
...
has_one :generic, as: generable, dependent: :destroy
end
class Cat < ActiveRecord::Base
...
has_one :generic, as: generable, dependent: :destroy
end
Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails polymorphic-associations ruby-on-rails-3 ruby-on-rails-4
我已经运行了这个迁移:
class AddUniqueToLocationColumnName < ActiveRecord::Migration
def change
remove_index :locations, :name
add_index :locations, :name, unique: true
end
end
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试回滚,但显示错误:
标准错误:发生错误,此迁移和所有后续迁移均已取消:remove_index 仅在给定 :column 选项时才可逆。
如何将此迁移回滚到我以前的版本?
想象一个具有以下提交的 git 存储库:
Fixed issue 3 123eabc
Fixed issue 2 (part 2) fa23b79
Fixed issue 2 (part 1) 7bea5cc
Fixed issue 1 0d229f4
Run Code Online (Sandbox Code Playgroud)
这些都已推送到远程主服务器。现在有没有办法重写历史并将中间的两个合并为一个提交?就像是
Fixed issue 3 123eabc
Fixed issue 2 9d23e0c
Fixed issue 1 0d229f4
Run Code Online (Sandbox Code Playgroud) 我的意思是。
我在这里实现了一个引导日期时间选择器。https://eonasdan.github.io/bootstrap-datetimepicker/
我的问题是。
如果你有一个输入框,如何禁用它并只让日期时间选择器的输入有效?因为用户可能输入了无效的日期格式。
下面的代码取自https://eonasdan.github.io/bootstrap-datetimepicker/#minimum-setup
代码:
<div class="container">
<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我想用空格分隔货币和总额.
请给我一个解决方案任何帮助将不胜感激.
p
strong Total:
span
= @order.currency
= humanized_money_with_symbol @order.total_paisas/100
Run Code Online (Sandbox Code Playgroud) 我有一个餐厅,购物车和用户模型.我想在进入餐厅时被重定向到购物车形式.此表格将询问我一个表号,然后为当前用户创建一个购物车.但是,在提交表单时,我遇到了传递参数的问题
Couldn't find Restaurant with 'id'=
Run Code Online (Sandbox Code Playgroud)
我想他们不在白名单中,但我找不到如何允许他们......我会感激任何建议!
通过的参数:
{"utf8"=>"?",
"authenticity_token"=>"*****",
"cart"=>{"restaurant_id"=>"1",
"table_id"=>"4"},
"commit"=>"Create Cart",
"user_id"=>"1"}
Run Code Online (Sandbox Code Playgroud)
我正在传递带有隐藏字段的restaurant_id和带有输入的表格
<%= f.hidden_field :restaurant_id, value: params[:restaurant_id] %>
<%= f.input :table_id %>
Run Code Online (Sandbox Code Playgroud)
在购物车控制器中:
def create
@restaurant = Restaurant.find(params[:restaurant_id])
@table = @restaurant.tables.find(params[:table_id])
@cart = current_user.carts.new(cart_params)
...
end
....
def cart_params
params.require(:cart).permit(:restaurant_id, :table_id)
end
Run Code Online (Sandbox Code Playgroud) 所以我有这个功能.
假设我有数百万的帖子.
我该如何优化此功能?
def fun
Post.all.each do |post|
if post.user.present?
post.active = true
else
post.active = false
end
post.save
end
end
Run Code Online (Sandbox Code Playgroud)
就像这样做更少,具有更好的性能,因为这不是一个非常好的方法.
ruby performance ruby-on-rails query-performance ruby-on-rails-4
控制器:
def show
@recipe = Recipe.find(params[:id])
@ingredients = @recipe.ingredients
@steps = @recipe.steps
end
Run Code Online (Sandbox Code Playgroud)
模板:
<%= @steps.each do |step| %>
<li>
<%= step.sequence %>
<%= step.description %>
</li>
<% end %>
Run Code Online (Sandbox Code Playgroud)
传递给我的模板的对象数组正在迭代,然后在结尾显示实际的字段数组(在图片的步骤5下面).我想显示每个对象的属性,而不是实际的对象列表.
我究竟做错了什么?
ruby ×3
erb ×1
git ×1
git-reset ×1
git-squash ×1
html ×1
javascript ×1
jquery ×1
parameters ×1
performance ×1
rollback ×1
slim-lang ×1
templates ×1