我是rails的新手,并尝试帮助解决问题。
我需要为来宾用户设置自定义路线,以查看来自特定子域的公开内容。即,当访客用户访问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时,他们会获得所需的结果,因为它将其路由到我的课程控制器中的自定义功能,并呈现我的custom.html.erb。但是,当他们单击自定义视图中的链接时,会将它们重定向到index.html.erb,而不是停留在此视图上,该视图具有部分html来呈现结果。
我希望他们会留在custom.html.erb上,所以我将额外的资源获取' / lessons /:lesson_category_id'添加到:'课程#custom' ,作为:'课程'希望这是它想要的。但没有运气。这是我在控制器和自定义视图中的自定义功能
自定义功能(想法是为访客用户提取信息。访客用户属于HBEC组织,因此他们登录hbec.myapp.com并获得正确的结果(hbec的类别列表和hbec的最新课程),当点击一个课程类别时,它应该向他们展示该类别中的课程(查询返回正确的结果,因为课程是最新的,当你点击类别它说什么都没有),而是显示什么都没有。因为它转到index.html.erb,而不是留在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
<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 :(得分:0)
事实证明,我的问题与丢失post和get之间的实例变量有关,所以通过确保每次页面重新加载时执行自定义控制器方法来解决这个问题,所有我必须添加的是:
before_filter:custom
这可确保在访客导航时刷新课程。
可能不是正确的方式,然而,它达到了我想要的效果,欢迎任何其他建议
感谢