1

에 의해 드롭 다운에서 인덱스 페이지에 깊은 중첩 된 자원을 정렬 그래서 나는 세 가지 모델이있다. 각 주제에는 많은 게시물이 있으며 각 게시물에는 많은 단락이 있습니다. 달성해야 할 주제는 단락을 주제별로 정렬하는 것입니다 (paragraphs/index.html.erb).레일 차 모델

<form>  
    <select> 
    <% @topics.sort { |a,b| a.name <=> b.name }.each do |topic| %> 
     <option><%= topic.name %></option> 
    <% end %> 
    </select> 
<input type="submit"> 
</form> 

나는에 조언을 따랐다 : 물론

나는 모든 주제를 포함하여 드롭 다운 메뉴가 Filter results on index page from dropdown을,하지만 난 주제 PARAMS 첫 번째 게시물에 연결하는 방법을 마련하기 위해 관리 할 수 다음 단락을 클릭하십시오. 나는 그걸로 어떻게 가야하는지 모를뿐입니다. 거기에는 많은 예제가없는 것처럼 보이므로 어떤 아이디어라도 대단히 감사합니다. 당신이 post.rb 및 topic.rb

확인에 accepts_nested_parameters_for ...를 지정한 경우

답변

1

시작하기 전에, 확인, 이제 우리는 마법이 발생하게 라우팅을 조정해야합니다. 그냥 routes.rb에 추가 :

patch 'paragraphs' => 'paragraphs#index' 
#we'll use PATCH for telling index which topic is active 

단락 # 지수는 동일하게 유지 :

def index 
    @topics = Topic.all 
end 

우리가보기에 할 것이다 나머지를. 따라서 index.html.erb :

<h1>Listing paragraphs sorted by Topic</h1> 

<% names_options = options_from_collection_for_select(@topics, :id, :name, selected: params[:topic_id]) %> 

<%= form_tag({action: "index"}, method: "patch") do %> 
    <%= select_tag :topic_id, names_options, 
       {prompt: 'Pick a topic', include_blank: false} %> 
    <%= submit_tag "Choose" %> 
<% end %> 

<% @topics = @topics.where(:id => params[:topic_id]).includes(:posts => :paragraphs) %> 

<% @topics.each do |topic| %> 
    <option><%= topic.name %></option> 
    <h2><%= topic.name %></h2> 
    <% topic.posts.each do |post| %> 
    <h3><%= post.content %></h3> 
    <% post.paragraphs.each do |paragraph| %> 
    <%= paragraph.content %><br> 
    <% end %> 
    <% end %> 
<% end %> 

비올라!

+0

여기에 있습니다. 고맙습니다! – Kasperi