使用 RJS/AJAX 更新多个 div
我成功地使用 RJS 在 page.replace.html create.js.rjs 上实现 AJAX。我正在尝试更新两个位置而不是一个,并且在观看 Ryan Bates Railscast 后我非常接近(我认为),但我的 /views/likes/create.js.rjs 文件的语法有问题。情况是这样的:
位于 /views/likes/create.js.rjs 的是以下代码:
page.replace_html "votes_#{ @site.id }", :partial => @like
page.replace_html "counter", 10 - (@question.likes.count :conditions => {:user_id => current_user.id})
page[@like].visual_effect :highlight
我的问题出在第二行。 div“计数器”在 /views/question/show.html.erb 页面中显示以下代码:
<div id="counter">
You have <%= 10 - (@question.likes.count :conditions => {:user_id => current_user.id}) %> votes remaining for this question
</div>
通过观看屏幕投射,我相信我的错误与第二行的语法有关。他特别提到您不能使用本地实例变量,但不确定如何进行更改。想法?
更新:这是我收到的错误:
ActionView::TemplateError (undefined method `likes' for nil:NilClass) on line #2 of app/views/likes/create.js.rjs:
1: page.replace_html "votes_#{ @site.id }", :partial => @like
2: page.replace_html "counter", 10 - (@question.likes.count :conditions => {:user_id => current_user.id})
3: page[@like].visual_effect :highlight
app/views/likes/create.js.rjs:2:in `_run_rjs_app47views47likes47create46js46rjs'
app/views/likes/create.js.rjs:1:in `_run_rjs_app47views47likes47create46js46rjs'
app/controllers/likes_controller.rb:8:in `create'
Rendered rescues/_trace (103.8ms)
Rendered rescues/_request_and_response (0.4ms)
Rendering rescues/layout (internal_server_error)
更新:
class LikesController < ApplicationController
def create
@user = current_user
@site = Site.find(params[:site_id])
@like = @site.likes.create!(params[:like].merge(:user_id => @user.id))
respond_to do |format|
format.html { redirect_to @site}
format.js
end
end
end
更新:这里是喜欢的形式:
<% remote_form_for [site, Like.new] do |f| %>
<%= f.hidden_field :site_name, :value => "#{site.name}" %>
<%= f.hidden_field :question_id, :value => @question.id %>
<%= f.hidden_field :ip_address, :value => "#{request.remote_ip}" %>
<%= f.hidden_field :like, :value => "1" %>
<%= submit_tag "^" , :class => 'voteup' %>
<% end %>
I am successfully using RJS to implement AJAX on a page.replace.html create.js.rjs. I am attempting to update two locations instead of one and after watching Ryan Bates Railscast I am very close (I think) but have a problem in the syntax of my /views/likes/create.js.rjs file. Here is the situation:
located at /views/likes/create.js.rjs is the following code:
page.replace_html "votes_#{ @site.id }", :partial => @like
page.replace_html "counter", 10 - (@question.likes.count :conditions => {:user_id => current_user.id})
page[@like].visual_effect :highlight
My problem lies in the second line. The div "counter" displays the following code in the /views/question/show.html.erb page:
<div id="counter">
You have <%= 10 - (@question.likes.count :conditions => {:user_id => current_user.id}) %> votes remaining for this question
</div>
From watching the screen cast I believe that my error has to do w/ the syntax of the second line. Specifically he mentions that you cannot use a local instance variable but not sure how to make the change. Thoughts?
UPDATE: Here is the error I am getting:
ActionView::TemplateError (undefined method `likes' for nil:NilClass) on line #2 of app/views/likes/create.js.rjs:
1: page.replace_html "votes_#{ @site.id }", :partial => @like
2: page.replace_html "counter", 10 - (@question.likes.count :conditions => {:user_id => current_user.id})
3: page[@like].visual_effect :highlight
app/views/likes/create.js.rjs:2:in `_run_rjs_app47views47likes47create46js46rjs'
app/views/likes/create.js.rjs:1:in `_run_rjs_app47views47likes47create46js46rjs'
app/controllers/likes_controller.rb:8:in `create'
Rendered rescues/_trace (103.8ms)
Rendered rescues/_request_and_response (0.4ms)
Rendering rescues/layout (internal_server_error)
UPDATE:
class LikesController < ApplicationController
def create
@user = current_user
@site = Site.find(params[:site_id])
@like = @site.likes.create!(params[:like].merge(:user_id => @user.id))
respond_to do |format|
format.html { redirect_to @site}
format.js
end
end
end
UPDATE: here is the likes form:
<% remote_form_for [site, Like.new] do |f| %>
<%= f.hidden_field :site_name, :value => "#{site.name}" %>
<%= f.hidden_field :question_id, :value => @question.id %>
<%= f.hidden_field :ip_address, :value => "#{request.remote_ip}" %>
<%= f.hidden_field :like, :value => "1" %>
<%= submit_tag "^" , :class => 'voteup' %>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试像这样获取@question,因为你的question_id将出现在params[:like]哈希中
无论如何,为了双重检查打印你的参数,所以在likes_controller的创建操作中添加一个puts语句。
您在第 #2 行遇到此错误(nil:NilClass 的未定义方法“likes”),
这意味着您的 @question 为零,您从未在创建操作中声明此实例。
希望这对你有用。
Try getting @question like this,as your question_id will be present inside params[:like] hash
Anyway for double checking print your params like this so add a puts statement in create action of likes_controller.
You have this error (undefined method `likes' for nil:NilClass) on line #2
Which means your @question is nil, you never declared this instance in your create action.
Hope this works for you.
你得到@question = nil。您应该检查@question是否为nil或@question.likes.count=0,如果是这样,您应该执行(10-0),即
OR
You are getting @question = nil. You should check @question is nil or @question.likes.count=0 and if so you should do (10-0) i.e.
OR
在控制器中,您需要加载
@question
变量。In controller you need to load
@question
variable.