내 웹 사이트의 홈 페이지에서 필터를 구현하고 싶습니다. 사용자는 많은 아이디어를 가지고 있습니다. 이 아이디어 표에는 아이디어의 유형을 결정하기 위해 "문자열", "iPhone 응용 프로그램", "Android 응용 프로그램"또는 "웹 응용 프로그램"중 하나가 저장되는 "플랫폼"이라는 열이 있습니다.레일 4 필터 홈페이지에서 모델의 피드 항목
사용자가 플랫폼을 기반으로 홈페이지에서 피드 항목을 필터링 할 수 있기를 원합니다.
내 아이디어 모델에서 특정 범위를 이미 만들었습니다 (아래에서 볼 수 있듯이). 이러한 범위는 콘솔에서 테스트 한 결과 작동합니다.
아이디어 모델
class Idea < ActiveRecord::Base
belongs_to :user
scope :iphone, -> { where(platform: 'iPhone application') } # All iphone ideas
scope :android, -> { where(platform: 'Android application') } # All android ideas
scope :web_app, -> { where(platform: 'Web Application') } # All webapp ideas
end
사용자 모델
class User < ActiveRecord::Base
has_many :ideas, dependent: :destroy
def feed
Idea.all
end
end
정적 페이지 컨트롤러 당신이 메신저가 그것에게 쉬운 방법 (특정에 대한 별도의 조치를하고, 아래에서 볼 수 있듯이 플랫폼 필터) 액션을 드롭 다운으로 연결하는 방법이나 홈페이지에서 iPhone 아이디어 만로드하는 버튼을 알아낼 수 없습니다.
class StaticPagesController < ApplicationController
def home
if signed_in?
@idea = current_user.ideas.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
def home_iphone
if signed_in?
@idea = current_user.ideas.build
@feed_items = current_user.feed.iphone.paginate(page: params[:page])
end
render "static_pages/home"
end
end
홈 페이지 여기 당신은 아이폰 아이디어에 대한 링크를 볼 수 있습니다 (순간에 오류가되지 않지만, 부하 모든 아이디어와뿐만 아니라 아이폰 아이디어)
<% if signed_in? %>
<%= link_to "iPhone", root_path, :controller => :ideas_controller, :action => :home_iphone %>
<div class="span8">
<h3>All ideas</h3>
<%= render 'shared/feed' %>
</div>
답해 주셔서 감사합니다. 그래서 링크 자체에서 current_user.ideas.iphone을 넣을 수 있다고 생각하십니까? 또는 컨트롤러에서 의미합니까? 조금 더 구체적 일 수 있습니까? –
컨트롤러에서 더 구체적인 동작 (home_iphone)이 될 것입니다. –