使用Ajax替换基于Onclick事件的页面元素

时间:2009-09-23 18:54:19

标签: ruby-on-rails ajax partials page-replacement

我正在重新提出这个问题,因为我现在更好地理解这个问题了。

我有一个应用程序有四个模型:用户,产品,品种和季节。

用户
has_many :seasons
has_many :products, :through => :seasons
has_many :varieties, :through => :seasons

产品
has_many :seasons
has_many :users, :through => :seasons
has_many :varieties

品种
belongs_to :product
has_many :seasons
has_many :users, :through => :seasons

四季
belongs_to :product
belongs_to :user
belongs_to :variety

在产品/展示视图中,我列出了特定产品的可用品种,以及携带该特定产品的用户。

首次加载产品/展示页面时,我会显示所有带有产品的用户:

<% @product.seasons.each do |c| %>  
  <%= link_to c.user.name, c.user %>  
<% end %>

我还列出了产品可用的品种:

<% @product.varieties.each do |c| %>  
  <%= link_to_remote variety.name, :url => variety_path(variety) %>  
<% end %>  

这是Ajax需要发生的地方。当我点击各种名称时,我想用@ variety.seasons替换@ product.seasons循环。我认为这应该只显示具有特定种类的用户。所以这看起来像这样:

<% @variety.seasons.each do |c| %>  
  <%= link_to c.user.name, c.user %>  
<% end %>     

我越来越近了,但我无法让它发挥作用。目前,当我点击一个品种时,前端没有任何反应。在日志中,我得到了这个:

Processing VarietiesController#4 (for ip at 2009-09-24 16:02:29) [POST]
  Parameters: {"authenticity_token"=>"9pDgKrHNbg0aMklsQsGl5BQa34bfr0Ft8Fpbxki3XXw="}
ActionController::UnknownAction (No action responded to 4. Actions: check_vendor_role, index, and show)  

日志引用#4并将4引用为动作。 4是我点击的项目的品种ID。

这是我的设置:

的ProductsController#节目
      @product = Product.find(params [:id])
      @varieties = @ product.varieties       @users = @ product.users

产品/展示视图     #列出拥有此产品的用户

<div id="userList">  
  <%= render :partial => "products/users" %>  
</div>  

#Lists the varieties available for this product  

<% @product.varieties.each do |variety| %>  
  <%= link_to_remote variety.name, :url => variety_path(variety) %>  
<% end %>  

VarietiesController#节目。

  def show
   @variety = Variety.find(params[:id])
    respond_to do |format|
      format.html
      format.xml  { render :xml => @variety}
      format.js
    end
  end

品种/ show.js.rjs
    @ page.replace_html“userList”,:partial =&gt; “产品/用户”,:collection =&gt; @ variety.users

因此,目前,该页面正在呈现并显示正确的html和数据。但是onclick没有运作。当我点击各种各样时,应用程序无法正确处理请求,用户列表也不会更改。

更新

回应Anatoliy的评论:

我不确定究竟需要添加哪条路线。我尝试添加以下内容,但结果与之前相同:

map.resources:variety,:member =&gt; {:show =&gt; :get}

您能否更具体地说明应该调用哪条成员路线?

2 个答案:

答案 0 :(得分:1)

如评论中所述(应列为答案):

添加:method =&gt; :转到你的link_to_remote调用,使用正确的HTTP动词对该URL进行“show”。

答案 1 :(得分:0)

&lt; div id =“user List”&gt;应该是&lt; div id =“users”&gt;