开发者

Basic Ruby on Rails AJAX Error

开发者 https://www.devze.com 2023-03-09 22:31 出处:网络
I\'m working through Agile Web Development with Rails, Edition 4 with some tweaks (mostly just naming variations), and I\'ve arrived at Iteration F2. In this iteration, you modify the index button wit

I'm working through Agile Web Development with Rails, Edition 4 with some tweaks (mostly just naming variations), and I've arrived at Iteration F2. In this iteration, you modify the index button with :remote => true, you add format.js to the respond_to section of the controller, and you generate a js.rjs file to execute the AJAX render. Or at least that's my interpretation of it. The goal of these steps is to have a cart (in this case, a team) in the sidebar update using AJAX when adding new line items (in this case, members)

In my case, I'm trying to add members to a team. Her's some code snippets I've added:

index.html.erb:

<%= button_to 'Add to Team', members_path(:player_id => player), 
    :remote => true %>

members_controller:

def create
    @team = current_team
    player = Player.find(params[:player_id])
    @member = @team.add_player(player.id)

    respond_to do |format|
      if @member.save
        format.html { redirect_to(nba_url) }
        format.js
        format.xml  { render :xml => @member, 
            :status => :created, :location => @member } 
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @member.errors, 
            :status => :unprocessable_entity }
      end
    end
end

create.js.rjs:

page.replace_html('team', render(@team))

The page is able to render, and I'm still able to click the button to add members to the team. However, the AJAX isn't working. When I reload, I can still see that the members have been added in the sidebar. All of the other team functionality remains, as I'm able to empty the team and add whichever members I wish. When I check the server log, I find the following error:

Error:

ActionView::Template::Error (undefined local variable or method `page' for #<
#<Class:0x413e1b8>:0x413cb20>):
    1: page.replace_html('team', render(@team))
  app/views/members/create.js.rjs:1:in `block in _app_views_members_create_js_rj
s___908569197_34199712_807066544'
  app/views/members/create.js.rjs:1:in `_app_views_members_create_js_rjs___90856
9197_34199712_807066544'
  app/controllers/members_controller.rb:47:in `create'

Based on this it seems like it has found the create.js.rjs but is having trouble interpreting it. I'm not sure what the weird symbols are in fr开发者_如何学Pythonont of page.

Edit: I've also found that if I view the source code before and after clicking the button, the button is indeed refreshing the code and adding the desired items. The problem seems to be exclusively in trying to refresh the partial.

Any help is appreciated. Thanks!


It seems your rjs file has some invalid bits at the start. Maybe try to re-create the file?


What did you expect to do with render(@team) ?

I've taken a look at the action view's method "render" and didn't found how you were expecting it to function. Maybe there is another functionality that you are aware and I don't.

You can also use erb and not rjs, using it just like a view


Along the lines of Bert Goethal's answer, is your editor saving your text file as UTF-8 with BOM?

A BOM will add two unicode encoded characters to the beginning of the file, and that might be where those are coming from...

0

精彩评论

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

关注公众号