1

I am still working on my website with a database containing items. In my model I've got parameters for records like: 'type' (melee, ranged, etc.etc.), 'ammunition', 'weight', 'damage' and etc. Now not every record has the same parametres, some have ammunition and some don't (they are just blank). Now I am trying to create a site in html.erb that shows the scope for the type of item we've choosen before by clicking the type button on index page. Let's say that we choose Melee weapons so it redirects us to page and the table only contains name, damage and weight. Next time we choose ranged and now the same page displays a table with name, damage, weight, AND ammunition capacity. Is this possible to do or do I have to create separate pages for every scope I have? If it is possible then can anyone help me with it, please?

Here's my model:

class Items < ActiveRecord::Base

  has_attached_file :icon, :styles => { :medium => "300x300>", :thumb => "24x24>" },
                    default_url: "/assets/items/missing.png",
                    url: "/assets/items/:id/:style/:basename.:extension",
                    path: ":rails_root/public/assets/items/:id/:style/:basename.:extension"
  validates_attachment_content_type :icon, :content_type => /\Aimage\/.*\Z/


  scope :melee, -> { where(type: 'Melee')}
  scope :ranged, -> { where(type: 'Ranged')}
  scope :accs, -> { where(type: 'Accessories')}
  scope :ves, -> { where(type: 'Vests')}

end

And here's the controller for the page:

def db_showspec
        @item = Items.melee
end

Once again sorry for my poor English!

  • Check my answer to this almost identical question http://stackoverflow.com/questions/27512831/displaying-results-from-scopes-in-rails/27512957#27512957. – ichigolas Dec 18 '14 at 13:44
  • Thank you, I've got it working but I still can't figure out how to make the table to show the fields fitting to the item type. –  Dec 18 '14 at 15:43

2 Answers2

1

Have a look at this answer. Using the same approach. You can have something like this in your routes:

resources :items do
  collection do
    get '/:type' => 'items#item_type'
  end
end

then in your ItemsController create a method:

def item_type
  @items = Post.respond_to?(params[:type])? Post.public_send(params[:type]) : []
end

now in your items/item_type.html.erb file:

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <% @items.each do |item| %>
      <tr>
        <td><%= item.name %></td>
        <td><%= item.price %></td>
      <tr>
    <% end %>
  </tbody>
</table>

code presented above is an example, of course you'd need to change attribute names and column names shown in above to suite with your scenario.

Community
  • 1
  • 1
Surya
  • 15,703
  • 3
  • 51
  • 74
  • Thank you! This is exactly what I was thinking about but I had totally not idea how to write it! I will update my post if I run into any problems. –  Dec 18 '14 at 15:01
  • It works, but there's still one thing i can't figure out. If I send the type "ranged" how do I make the unique for ranged items parameters to appear (like ammunition)? I was thinking about <% if Items.type = ranged %> blablabla <% end %> but it also appears while I scope for the melee items. –  Dec 18 '14 at 15:36
0
<table>

  <% @items.each_slice(3) do |slice| %>   
    <tr>
      <% slice.each do |item| %>
       <td> 
         <%= item.name %>
       </td>
     <% end %>
    </tr>
  <% end %>

</table>
djadam
  • 649
  • 1
  • 4
  • 20