使用 Ajax 根据 Onclick 事件替换页面元素

发布于 2024-08-05 19:53:33 字数 2608 浏览 4 评论 0原文

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

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

用户
has_many:季节
has_many:产品,:通过 => :季节
has_many:品种,:通过 => :季节

产品
has_many:季节
has_many :用户,:通过 => :季节
has_many :varieties

品种
属于:产品
has_many :季节
has_many :用户,:通过 => :季节

季节
属于:产品
属于:用户
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 的地方。当我单击品种名称时,我想将 @product.seasons 循环替换为 @variety.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#show
@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#show。

  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 => “产品/用户”,:集合=> @variety.users

因此,当前页面正在渲染并显示正确的 html 和数据。但 onclick 不起作用。当我单击各种时,应用程序无法正确处理请求,并且用户列表不会更改。

更新

对阿纳托利评论的回应:

我不确定到底需要添加什么路线。我尝试添加以下内容,但结果与以前相同:

map.resources :varieties, :member => {:显示=> :get }

你能更具体地说明应该调用什么成员路由吗?

I'm reformulating this question as I'm understanding the issue better now.

I have an application with four models: Users, Products, Varieties and Seasons.

User
has_many :seasons
has_many :products, :through => :seasons
has_many :varieties, :through => :seasons

Product
has_many :seasons
has_many :users, :through => :seasons
has_many :varieties

Variety
belongs_to :product
has_many :seasons
has_many :users, :through => :seasons

Seasons
belongs_to :product
belongs_to :user
belongs_to :variety

On the product/show view, I list each variety available for a particular product, and the users who carry that particular product.

When the product/show page first loads, I display all users who carry the product by doing this:

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

I also list the varieties available for the product by doing this:

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

Here's where the Ajax needs to happen. When I click on a variety name, I want to replace the @product.seasons loop with a @variety.seasons. I think this should just display only the users who have that particular variety. So that would look like this:

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

I'm getting closer, but I can't get this to work. Currently, when I click on a variety, nothing happens on the frontend. In the log, I get this:

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)  

The log refers to #4 and refers to 4 as an action. 4 is the variety ID of the item that I clicked on.

Here's my setup:

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

Product/show view
#Lists the users who have this product

<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#show.

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

varieties/show.js.rjs
@page.replace_html "userList", :partial => "products/users", :collection => @variety.users

So, currently, the page is rendering and displaying the proper html and data. But the onclick is not functioning. When I click a variety, the the app does not properly process the request and the user list does not change.

Update

Response to Anatoliy's comment:

I'm not sure exactly what route needs to be added. I tried adding the following, but the result was the same as before:

map.resources :varieties, :member => { :show => :get }

Could you be more specific about what member route should be called?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的心爱 2024-08-12 19:53:33

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

添加 :method => :get 到您的 link_to_remote 调用,以针对该 URL 使用正确的 HTTP 动词“显示”。

As Stated in the comments (which should be listed as an answer):

Add :method => :get to your link_to_remote call to use the correct HTTP verb for "show" against that URL.

黑凤梨 2024-08-12 19:53:33

应该是

<div id="userList"> shoul be <div id="users">

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文