I'm hacking away at a rails project and I wanted to modify the number of items that end up on a particular page. The page gets populated via an array of items.
For the life of me I can't figure out how to make it show only 2 instead of 4 items.
In the haml file there is this section:
%ul.story-list
- @stories.each do |story|
%li
- unless story.image.blank?
.img-container{ class: ((story.video.blank?)? "": "video-container") }
= image_tag(story.image_url, alt: story.name, class: ((story.video.blank?)? "": "js-has-video"), :video => story.video)
.story-data
%h4= story.name
%h5.location= story.location
%p.quote= story.story
- if story.get_connected?
= link_to 'Get Connected', connect_path
- elsif story.gather_supplies?
= link_to 'Gather Supplies', supplies_path
- elsif story.make_a_plan?
= link_to 'Make a plan', plan_path
The page shows up (on the server) with four story items, I want it to only show two. I was expecting to open the haml file and just delete some lines (or comment them out). I'm so confused.
So, I suspect the number of stories comes from a controller or something like that. ..but maybe it is coming from the placeholder data on the server?
あなたが私を助けてくれるように促された場合、すべてのコードはここにありますhttps://github.com/city72/city-72
私が変更しようとしている正確なページはこれです。2つのストーリーだけを含めたいです:http://collier72.herokuapp.com/stories
奇妙なことに、私のローカル環境では、ストーリーをまったく編集できません。それが、アイテムの数がデータから来ていることを私に教えてくれます。
ストーリーコントローラーは、ストーリーの数を指定しないこの小さなファイルです。
class StoriesController < ApplicationController
after_filter :static_content
def index
all_stories = EmergencyStory.order("index,id ASC").all
@selected_story = all_stories.select {|s| s.selected}.first
@stories = all_stories.collect.select {|s| !s.selected}
end
end
このファイルを開きます。
https://github.com/city72/city-72/blob/master/app/controllers/stories_controller.rb#L8
これからその行を変更します:
@stories = all_stories.collect.select {|s| !s.selected}
これに:
@stories = all_stories.collect.select{|s| !s.selected}.slice(0,2)
私の知る限り、4が返されるという事実は意図的なものではなく、データベースにあるものにすぎません。slice(0,2)
最初の2つの項目を返します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加