Reputation: 81
I have a bunch of artworks. I want to be able show only those artworks which are sold (which is a column in my table, :availability='Private collection'). How would I do this? This is what I've tried so far: Create a 'sold' action in the artworks_controller.rb:
def index
if params[:tag]
@artworks = Artwork.tagged_with(params[:tag])
elsif params[:series]
@artworks = Artwork.tagged_with(params[:series])
elsif params[:availability]
@artworks = Artwork.where(availability: 'Private collection')
else
@artworks = Artwork.all
end
end
def sold
@artworks = Artwork.where(availability: 'Private collection')
end
routes.rb:
get 'sold' => 'artworks#index', :as => 'sold'
I know that I have artworks tagged as these separate options and they're showing up correctly on my show page (this is my form):
<%= f.input :availability, :collection => ['Available for purchase','Private collection','Not for sale'], :as => :radio_buttons %>
All this doesn't seem to break anything, but neither does it allow me to see only those artworks which are sold when I got to http://localhost:3000/sold. What am I doing wrong?
Upvotes: 1
Views: 485
Reputation: 10416
This currently routes to the articles index:
get 'sold' => 'artworks#index', :as => 'sold'
As a result it's using your query in the index action, and with no params it gets all of them. So you need to route it to the sold action:
get 'sold' => 'artworks#sold', :as => 'sold'
Then in your controller you can tell rails to render the index view
def sold
@artworks = Artwork.where(availability: 'Private collection')
render :index
end
Which will use the index view but with the query from the sold action.
Upvotes: 2