开发者

Ajax and vote_fu | missing before statement

relate question: Vote_fu and Ajax requests

There seems to be something wrong with my Ajax request. What I 'm trying to do is on event click vote submit a vote then update page with out refreshing the page.

votes_controller.rb:

def create
    @album = Album.find(params[:album_id])

    respond_to do |format|
      if current_user.vote(@album, params[:vote])
        format.js  { render :action => "create", :vote => @vote }
        format.html { redirect_to([@album.user, @album]) }
        #format.xml  { render :xml => @album, :status => :created, :location => @album }
      else
        format.js  { render :action => "error" }
        format.html { render :action => "new" }
        format.xml  { render :xml => @vote.errors, :status => :unprocessable_entity }
      end
    end
  end

link | for view album show :

<%= link_to_remote "Vote Up",
:url => user_album_votes_path(album.user, album, 
:vote => :true, :format => :js),
:method => :post %>

application.js

jQuery.ajaxSetup({
    'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

jQuery(document).ready(function() {
  $("#votes_.album").bind('click');
})

create.js

page.replace_html "#votes_{@album.id}", 
:partial => "album_vote", 
:locals => {:album => @album}

This is the following error message which I'm getting:

missing ; before statement
[Break on this error] page.replace_html "#votes_#{@album.id}", ...bum_vote", :locals => {:album => @album}

I'm not sure what is going wrong here I have been following many example from the vote_fu doc's still having problems.

http://github.com/peteonrails/vote_fu/tree#readme

one amendment made on create.js

there is now another error:

No the error has moved over to the votes_controller

NoMethod开发者_StackOverflow社区Error (You have a nil object when you didn't expect it!
<br />
The error occurred while evaluating nil.vote):
  app/controllers/votes_controller.rb:53:in `create'
  app/controllers/votes_controller.rb:52:in `create'
<br />
Rendered rescues/_trace (128.4ms)
Rendered rescues/_request_and_response (0.4ms)
Rendering rescues/layout (internal_server_error)

These lines are on the create action, which looks perfectly fine!?

How do I get this to work?

Regard

Dan


Try changing the create.js to

page.replace_html "#votes_#{@album.id}", :partial => "album_vote", :locals => {:album => @album}

You might have missed the # for the variable string interpolation.


Solved!

The problem was that I didn't add a before statement to refresh the vote count! so I did and it worked as well as that, I changing the create.js to create.sj.erb, also I made some small changes to my application.js file. After all that I then added a flash[:note] = you have voted!, then added a function to remove the flash notice after a sec and fadeOut!

For anyone who's interested heres the code:

Application.js

jQuery(document).ready(function() {
  $("#vote").bind('click', function(e) {
    if (e.target.tagName == "DIV") {
        $(this).find(".album_extened").toggle('blind');
    }
 })

})

create.js.erb

$("#vote").before('<div id="notice"><%= escape_javascript(flash.delete(:notice)) %></div>');
$("#vote_count").html("<%= @album.votes_for - @album.votes_against %>");

$(document).ready(function() {
  setTimeout(hideFlashMessages, 1000);
});

function hideFlashMessages() {
$("#vote, #notice").append('').fadeOut(1000);
}

If anyone knows a better why which this can be done please forward!!

A good viewing of [http://railscasts.com/episodes/136-jquery][1]

Reading of [link text][2]

Soon fixed things Thanks Jonathanlink text

[1]: http://railscasts.com/episodes/136-jquery/"Rails Cast ep 136" [2]: http://www.notgeeklycorrect.com/english/2009/05/18/beginners-guide-to-jquery-ruby-on-rails/"Beginners Guild to jQuery and Rails" and Thanks Sam for being helpful!!


It seems to me that the current_user in the create method did not get its value set.

How do you set the current_user value? In other plugins, they usually define this as an instance method or instance variable with accessor.

So maybe changing the create method to the following might help:

def create
    @album = Album.find(params[:album_id])

    respond_to do |format|
      if @current_user.vote(@album, params[:vote])
        format.js  { render :action => "create", :vote => @vote }
        format.html { redirect_to([@album.user, @album]) }
        #format.xml  { render :xml => @album, :status => :created, :location => @album }
      else
        format.js  { render :action => "error" }
        format.html { render :action => "new" }
        format.xml  { render :xml => @vote.errors, :status => :unprocessable_entity }
      end
    end
  end

You might have missed the @ before the current_user.vote

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜