使用 RJS/AJAX 更新多个 div

发布于 2024-09-02 12:20:12 字数 2432 浏览 7 评论 0原文

我成功地使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪也成诗 2024-09-09 12:20:12

尝试像这样获取@question,因为你的question_id将出现在params[:like]哈希中

 @question = Question.find params[:like][:question_id]

无论如何,为了双重检查打印你的参数,所以在likes_controller的创建操作中添加一个puts语句。

puts params.inspect,params[:like][:question_id]

您在第 #2 行遇到此错误(nil:NilClass 的未定义方法“likes”),

这意味着您的 @question 为零,您从未在创建操作中声明此实例。

希望这对你有用。

Try getting @question like this,as your question_id will be present inside params[:like] hash

 @question = Question.find params[:like][:question_id]

Anyway for double checking print your params like this so add a puts statement in create action of likes_controller.

puts params.inspect,params[:like][:question_id]

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.

蘑菇王子 2024-09-09 12:20:12

你得到@question = nil。您应该检查@question是否为nil或@question.likes.count=0,如果是这样,您应该执行(10-0),即

if //your condition here 
  page.replace_html  "counter", "You have 10 votes remaining for this question"
else 
  page.replace_html  "counter", 10 - (@question.likes.count :conditions => {:user_id => current_user.id})

OR

remaining_votes= (@question && [email protected] && @question.likes.count :conditions => {:user_id => current_user.id} !=0 )? @question.likes.count :conditions => {:user_id => current_user.id} : 0
page.replace_html  "counter", "You have #{10-remaining_votes } votes remaining for this question"

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.

if //your condition here 
  page.replace_html  "counter", "You have 10 votes remaining for this question"
else 
  page.replace_html  "counter", 10 - (@question.likes.count :conditions => {:user_id => current_user.id})

OR

remaining_votes= (@question && [email protected] && @question.likes.count :conditions => {:user_id => current_user.id} !=0 )? @question.likes.count :conditions => {:user_id => current_user.id} : 0
page.replace_html  "counter", "You have #{10-remaining_votes } votes remaining for this question"
尘世孤行 2024-09-09 12:20:12

在控制器中,您需要加载 @question 变量。

In controller you need to load @question variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文