views:

195

answers:

2

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?

A: 

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

Anatoliy
That was a typo in my question. I fixed it. Any other ideas?
MikeH
In config/routes.rb should be declared appropriate action in :member section of controller (to avoid error UnknownAction No action responded to 4.)
Anatoliy
I added an update to follow up on this. I'm not sure exactly what route you're thinking should be added.
MikeH
I think I found the problem. Ajax uses POST by default, but your routes expects GET, you shoud fix link like that: :html => { :method => :get } (i'm not shure syntax, but basically idea)
Anatoliy
This was right. Thanks!
MikeH
+1  A: 

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.

Mark S.
Yes! This was the issue. Thanks.
MikeH