I am trying to get a grasp of Ajax response in ROR. Following the examples in the ROR API and the books Simply Rails 2 from Sitepoint, following are the code snippets from votes_controller and create.js.rjs:
(from app/controllers/votes_controller.rb) def create @story = Story.find(params[:story_id]) @story.votes.create(:user => @current_user)
respond_to do |format| format.html { redirect_to @story } format.js end end
(from app/views/votes/create.js.rjs) page.replace_html 'vote_score', "Score: #{@story.reload.votes.size}" page[:vote_score].visual_effect :highlight
Following is the code from (app/views/stories/show.html.erb) that triggers the create action in the votes controller: <div id="vote_form"> <% form_remote_for [ @story, Vote.new ] do |f| %> <%= f.submit 'shove it' %> <% end %> </div>
When I hit the above shove it button, I am getting the following error (from the firebug console window) <h1>NameError in Votes#create</h1> <p>Showing <i>votes/create.js.rjs</i> where line <b>#1</b> raised: <pre><code>undefined local variable or method `page' for #<ActionView::Base:0x46caf94></code></pre> </p>
Instead when I change the create def to include the inline JavaScript statements as follows, its working:
def create @story = Story.find(params[:story_id]) @story.votes.create(:user => @current_user)
respond_to do |format| format.html {redirect_to @story} format.js { render :update do |page| page.replace_html 'vote_score', "Score: #{@story.reload.votes.size}" page[:vote_score].visual_effect :highlight } end end
Looks like the JavascriptGenerator is generating the necessary JavaScript if the statements are inline instead of in a separate view file. Is is behavior expected.
PS: I am working on WinXP using InstantRails with Rails 2.1
Thanks in advance.