rails ajax和partials

时间:2009-10-09 21:34:27

标签: ruby-on-rails ajax

我无法进行部分迭代 - 我传递的对象永远不会与部分变量匹配

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

部分代码

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

错误

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'

1 个答案:

答案 0 :(得分:3)

Story.find(:all)返回一个数组。所以实际上当你试图获得title属性时,你试图从数组中获取它。

如果您只想选择一个故事或者可以替换

,您可以执行Story.find(:first)
:object => @story

:collection => @story 

渲染多个故事。