# Problem with Ajax response with the use of action.js.rjs file

**URL:** <https://www.ruby-forum.com/t/problem-with-ajax-response-with-the-use-of-action-js-rjs-file/141305>\
**Category:** Rails\
**Created:** [June 24, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/problem-with-ajax-response-with-the-use-of-action-js-rjs-file/141305 "2008-06-24T15:52:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 24, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/problem-with-ajax-response-with-the-use-of-action-js-rjs-file/141305/1 "2008-06-24T15:52:56Z")

</div>

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:

 \<% form\_remote\_for [@story, Vote.new] do |f| %\> \<%= f.submit 'shove it' %\> \<% end %\>

When I hit the above shove it button, I am getting the following error  
(from the firebug console window)

# NameError in Votes#create

Showing _votes/create.js.rjs_ where line **#1** raised:

```
undefined local variable or method `ï»¿page' for
#<ActionView::Base:0x46caf94>
```

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.
