2015-04-03 63 views
1

我是rails新手,並試圖幫助解決問題。Ruby中的自定義路由

我需要爲訪客用戶設置一個自定義路由,以查看來自特定子域的公共內容。即當訪客用戶轉到subdomain.myapp.com時,他們將看到訪客內容,如果他們登錄,他們將看到特定於其角色的內容並添加內容。所以,我已經建立了以下路線:

match '/', to: "lessons#custom", constraints: { subdomain: 'hbec2' }, via: [:get] 

    match '/', to: 'home#index', constraints: { subdomain: 'www' }, via: [:get] 
    root :to => "home#index" 

    get "welcome" => "lessons#welcome", :as => "welcome" 

    constraints subdomain: 'hbec2' do 
    resources :home do 
     collection do 
     get '/lessons/:lesson_category_id', to: 'lessons#custom', as: 'lessons' 
     get "custom" 
     get "load_categories" 
     get "load_subcategories" 
     get "load_child_subcategories" 
     end 
    end 
    end 

    constraints subdomain: 'www' do 
    resources :home do 
     collection do 
     get "sub_categories" 
     get "categories" => "lessons#welcome" 
     get "welcome" 
     end 
    end 
    end 

目前,當用戶去hbec2.myapp.com他們得到期望的結果,因爲它的路線是在我的教訓控制器我的自定義功能,使我的自定義.html.erb。但是,當他們點擊自定義視圖中的鏈接時,它們將被重定向到index.html.erb而不是保留在該視圖上,該視圖具有部分html來呈現結果。

我所希望的是他們會留在custom.html.erb中,所以我添加了額外的資源獲得'/ lessons /:lesson_category_id',以:'lessons#custom',如:'lessons'希望是它會尋找的。但沒有運氣。這裏是我的自定義功能從控制器和自定義視圖

自定義功能(這個想法是爲來賓用戶提供信息。來賓用戶屬於HBEC組織,因此他們登錄到hbec.myapp.com並獲得正確的結果(hbec的類別列表和hbec最近的課程列表),當單擊課程類別時,應該向他們顯示該類別中的課程(查詢返回正確的結果,因爲課程是最近的,當您單擊類別它說什麼也沒有),而是顯示他們什麼都沒有。因爲它去,而不是停留在custom.html.erb

def custom 
    params[:per_page] = 5 
    @organization = Organization.find_by_id(HBEC_ORG_ID) 
    @organization_categories = [] 
    @categories = LessonCategory.all 
     @organization_categories = @categories.where("organization_id = #{HBEC_ORG_ID}") 
    @categories = @categories.where("organization_id = #{HBEC_ORG_ID}").limit(15) 

    @lesson_category = LessonCategory.find_by_id(params[:lesson_category_id]) unless params[:lesson_category_id].blank? 
    @lesson_subcategory = LessonSubCategory.find_by_id(params[:lesson_sub_category_id]) unless params[:lesson_sub_category_id].blank? 

    keyword = params[:keyword] unless params[:keyword].blank? 
    @lessons = Lesson.scoped 
    # @lessons = @lessons.where("is_disable = ? ", false) 
     @lessons = @lessons.where("organization_id = #{HBEC_ORG_ID}") 


    @lessons = @lessons.where(["lessons.id = ? OR lessons.name LIKE ? ", keyword.to_i, "%#{keyword}%"]) unless keyword.blank? 
    @lessons = @lessons.where(["lesson_category_id = ?", params[:lesson_category_id]]) unless params[:lesson_category_id].blank? 
    @lessons = @lessons.where(["lesson_sub_category_id = ?", params[:lesson_sub_category_id]]) unless params[:lesson_sub_category_id].blank? 

    params[:page] = 1 if params[:page].blank? 
    max_pages = (@lessons.count/per_page.to_i).to_i + 1 
    params[:page] = max_pages if params[:page].to_i > max_pages 
    @lessons = @lessons.order(sort_column + ' ' + sort_direction + ', lessons.created_at DESC').paginate(:per_page => per_page, :page => params[:page]) 
    @current_page = params[:page] 
    @toal_pages = max_pages 
    end 

custom.html.erb

0至index.html.erb,
<div class="pages page-work gray-bg medium-grey" id=""> 
    <div class="container"> 
    <!-- Header --> 
    <header> 
     <div class="span-12"> 
     <div class="span7 lesson_header"> 
      <%= render "/lessons/breadcrum" %> 

     </div> 
     <div class="span5 pull-right lesson_header"> 
      <%= render "lessons/lesson_header" %> 
     </div> 
     </div> 
    </header> 
    <!-- End Header --> 


    <!-- Article --> 
    <article class="showdetail"> 


     <div><%= render "shared/flash_messages" %></div> 
       <%= render "/organizations/organization_categories", :organization => @organization, :organization_categories => @organization.lesson_categories %> 
    </article> 


<!-- recommended not necessary for hbec 
    <%= render :partial => "/lessons/recommended" %> 
--> 
    <p class="adjust">Most Recent Lessons</p> 
    <ul class="cat_thumbnails plugin-filter-elements portfolio-items" id="items"> 
     <% if @lessons.present? %> 
      <% @lessons.each do |lesson| %> 
       <li class="col-sm-4 lessonpad mix illustration li-back"> 
       <a href="<%= "#{lesson_path(lesson)}" %>"> 
        <!-- Portfolio image --> 
        <% avatar = (lesson.image.present? ? "#{lesson.image.url}" : "/assets/placeholder_01.png") %> 
        <img src="<%= avatar %>" alt="Treble" class="recent-lessons-icon"> 

        <!-- Portfolio name, activated on hover --> 
        <h3 align="center" class="category-text"><%= lesson.title %></h3> 
       </a> 
       <%= render "latest_lessons", :lesson => lesson %> 
       </li> 

      <% end %> 
     <% else %> 
      <% 
      category_or_subcategory_name = "" 
      category_or_subcategory_name = " '#{@lesson_category.name}' " if @lesson_category.present? 
      category_or_subcategory_name = " '#{@lesson_subcategory.name}' " if @lesson_subcategory.present? 
      %> 
      <h3 class="error"> No <%= category_or_subcategory_name %> from custom Lessons were found</h3> 
     <% end %> 

    </ul> 
    <% if @lessons.present? && @current_page.to_i < @max_pages.to_i %> 
     <div class="center loadmore"> 
      <button class="btn btn-success load_more" onclick="load_more('/lessons', 'lessons')">Load More</button> 
     </div> 
    <% end %> 



    <% if @categories.present? %> 
     <p class="adjust">Choose a Category for Lessons</p> 
     <ul class="cat_thumbnails plugin-filter-elements portfolio-items" id=""> 
      <% @categories.each do |cat| %> 
       <li class="category-li col-sm-4 subpad mix illustration <%= cat.slag %> "> 

       <% link = cat.lesson_sub_categories.present? ? "/home/sub_categories?id=#{cat.id}" : " #{lessons_url(:lesson_category_id => cat.id)}" %> 

       <a href="<%= link %>"> 
        <!-- Portfolio image --> 
        <% avatar = (cat.avatar.present? ? "#{cat.avatar.url}" : "/assets/placeholder_01.png") %> 
        <img src="<%= avatar %>" class="choose-category-icon"> 

        <!-- Portfolio name, activated on hover --> 
        <h3 align="center" class="category-text"><%= cat.name %></h3> 
       </a> 
       <br> 
       </li> 
      <% end %> 
     </ul> 
    <% end %> 

    <% if @categories.present? && LessonCategory.all.count > 5 %> 
     <div class="center"> 
      <button class="btn btn-success load_more" onclick="window.location.href = '/home/categories'">Load More 
      </button> 
     </div> 
    <% end %> 
    </article> 


    </div> 
</div> 
<%= hidden_field_tag :total_pages, @toal_pages %> 
<%= hidden_field_tag :current_page, @current_page %> 

回答

0

所以事實證明,我的問題不得不做失去崗位的實例變量和得到的,所以確保我的自定義控制器的方法解決了這個問題,每次頁面重新加載執行,所有我不得不添加了:

的before_filter:定製

這保證了作爲來賓導航的教訓被刷新。

可能不是正確的方法,但是,它實現了我的願望的效果,歡迎任何額外提醒

感謝