【发布时间】:2015-07-18 14:28:36
【问题描述】:
我已阅读此主题的所有答案,但我请求您的帮助,因为我仍然没有找到解决方案。我创建了投票计数器,它将从模型增加喜欢计数器中读取并将更新的计数器++写入模型。
我有index.html.erb
<div class="row">
<div id="tasks"><%= render @like %></div>
</div>
<div class="row">
<div id="like-form"></div>
</div>
<div class="col-md-2 col-md-offset-4">
<%= link_to new_like_path, remote: true do %>
<button class="btn btn-default">New</button>
<% end %>
</div>
模型架构:
create_table "likes", force: true do |t|
t.string "count"
t.datetime "created_at"
t.datetime "updated_at"
end
我有LikesController
def new
if Like.first.count.to_i >= 0
then Like.create(count: "0")
end
@like = Like.first.count.to_i
@like++
Like.update(0, :count => @like.to_s)
end
new.js.rb文件
$('#like-form').html("<%= j (render 'form') %>");
$('#like-form').slideDown(350);
我有_form.html.erb
<%= @like %>
它不想渲染,我得到了
'nil' 不是 ActiveModel 兼容的对象。它必须执行 :to_partial_path.
我是 Rails 的新手,希望一切都有意义。我听说过实现此任务的不同方法,但请帮助我修复提供的方法:将带有 ruby 变量的表单呈现为 html。
【问题讨论】:
-
为什么
count列的类型是string? -
增量运算符 (++) 在 ruby 或 rails 中不存在。你必须使用
@like+=1。 -
我建议在转向 Rails 之前先回顾一下 Ruby 语法,尤其是正如 Menelik 所说,
++在 Ruby 中不存在,then也不是我在 IF 上看到的关键字,虽然有效,但看到它真的很奇怪。最后但不是列表,我相信你有一个大问题,就像new方法中的最后一个:Like.update(0, :count => @like.to_s)它甚至不清楚你想用它实现什么
标签: jquery ruby-on-rails ruby ajax