简体   繁体   中英

rails ajax and partials

I cant get a partial to iterate- the object I pass never matches the partial variables

The controller method for the ajax

 def news
  @fixture = Fixture.find(params[:fixture_id])
  home_team = Team.find(@fixture.home_team)
  away_team = Team.find(@fixture.away_team)

  @story = Story.find(:all, :conditions => "team_id = #{home_team.id}", :order => "dated DESC", :limit => 5)


   respond_to do |format|
    format.js do 
       render :update do |page|
       page.replace_html "home_id_fixture_#{@fixture.id}", :partial => "story/story", :object => @story
      end
    end
   end
 end

partial code

<div id="story-vote-<%= story.id %>">x</div>
<div id="story-title"><%= story.title %></div>

error

ActionView::TemplateError (undefined method `title' for []:Array) on line #3 of app/views/story/_story.erb:
1: 
2: <div id="story-vote-<%= story.id %>">x</div>
3: <div id="story-title"><%= story.title %></div>
4: 
5:   

    app/views/story/_story.erb:3
    app/controllers/hub_controller.rb:75:in `__instance_exec0'
    app/controllers/hub_controller.rb:74:in `news'
    app/controllers/hub_controller.rb:72:in `news'

Story.find(:all) returns an array. So actually when you try to get the title attribute you're trying to get it from an array.

You can either do Story.find(:first) if you only want to select one story or you can replace

:object => @story

with

:collection => @story 

to render multiple stories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM