- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我的应用程序中有这个非常大的 Controller 。我真的很想让它尽可能瘦。下面是一些代码,显示了我目前正在做的事情的类型。我想知道我可以从中移出哪些东西?
请注意 - 这不是我的确切代码,很多都是相似的。基本上每个实例变量都在 View 中使用——这就是为什么我不明白如何将逻辑放入模型中的原因?模型可以返回实例变量的值吗?
def mine
#For Pusher
@push_ch = "#{current_user.company.id}"+"#{current_user.id}"+"#{current_user.profile.id}"
#Creating a limit for how many items to show on the page
@limit = 10
if params[:limit].to_i >= 10
@limit = @limit + params[:limit].to_i
end
#Setting page location
@ploc="mine"
@yourTeam = User.where(:company_id => current_user.company.id)
#Set the user from the param
if params[:user]
@selectedUser = @yourTeam.find_by_id(params[:user])
end
#Get all of the user tags
@tags = Tag.where(:user_id => current_user.id)
#Load the user's views
@views = View.where(:user_id => current_user.id)
if !params[:inbox]
#Hitting the DB just once for all the posts
@main_posts = Post.where(:company_id => current_user.company.id).includes(:status).includes(:views)
@main_posts.group_by(&:status).each do |status, posts|
if status.id == @status.id
if @posts_count == nil
@posts_count = posts
else
@posts_count = @posts_count + posts
end
elsif status.id == @status_act.id
if @posts_count == nil
@posts_count = posts
else
@posts_count = @posts_count + posts
end
end
end
if params[:status] == "All" || params[:status] == nil
@posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).order(sort_column + " " + sort_direction).where(:company_id => current_user.company.id, :status_id => [@status.id, @status_act.id, @status_def.id, @status_dep.id, @status_up.id]).limit(@limit).includes(:views)
else
@posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).order(sort_column + " " + sort_direction).where(:company_id => current_user.company.id).limit(@limit).includes(:views)
end
elsif params[:inbox] == "sent"
@yourcompanylist = User.where(:company_id => current_user.company.id).select(:id).map(&:id)
@yourcompany = []
@yourcompanylist.each do |user|
if user != current_user.id
@yourcompany=@yourcompany.concat([user])
end
end
if params[:t]=="all"
@posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).tag_filter(params[:tag], current_user).order(sort_column + " " + sort_direction).where(:user_id => current_user.id).includes(:views, :tags).limit(@limit)
elsif params[:status]!="complete"
@posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).tag_filter(params[:tag], current_user).order(sort_column + " " + sort_direction).where(:user_id => current_user.id).includes(:views, :tags).limit(@limit)
elsif params[:status]!=nil
@posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).tag_filter(params[:tag], current_user).order(sort_column + " " + sort_direction).where(:user_id => current_user.id).includes(:views, :tags).limit(@limit)
end
end
respond_to do |format|
format.html # index.html.erb
format.js # index.html.erb
format.xml { render :xml => @posts }
end
end
最佳答案
您可以从将逻辑移入模型开始...
像这样的一行令人羡慕不已:
@push_ch = "#{current_user.company.id}"+"#{current_user.id}"+"#{current_user.profile.id}"
我建议将其移至模型中:
#user.rb
def to_pusher_identity
"#{self.company_id}#{self.id}#{self.profile_id}"
end
然后在你的 Controller 中
@push_ch = current_user.to_pusher_identity
此时您甚至可以将其移动到 before_filter
中。
before_filter :supports_pusher, :only => :mine
您可以做的另一件事是创建更丰富的联想,这样您就可以表达:
@tags = Tag.where(:user_id => current_user.id)
作为
@tags = current_user.tags
另一个例子是主要帖子,而不是
Post.where(:company_id => current_user.company.id).includes(:status).includes(:views)
你会经历这些联想:
current_user.company.posts.includes(:status).includes(:views)
关于ruby-on-rails - 在 rails 3 skinny 中制作胖 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7043268/
Chrome、IE 和 Safari 都按预期显示代码。但是,Firefox 为任何未包含在“p”标签中的文本提供“瘦列”。 但是它确实可以在较小的宽度下使用媒体查询正确显示。也许与 .event-c
我需要每天将 60GB 的数据插入 cassandra。 这分解为 100组 key 每组 150,000 个键 每个 key 4KB 的数据 就写入性能而言,我最好使用 每组 1 行,每行 150,
我的应用程序中有这个非常大的 Controller 。我真的很想让它尽可能瘦。下面是一些代码,显示了我目前正在做的事情的类型。我想知道我可以从中移出哪些东西? 请注意 - 这不是我的确切代码,很多都是
在 Java EE 项目中,我将所有库从 WAR/WEB-INF/lib 移动到 EAR/lib。 打开一个 JSP,现在我得到这个错误: Struts has detected an unhandl
我有两个模型: 服务器(属于从属) 从站(有许多服务器) 在 Controller 中,我需要获取一个包含服务器/从站信息的数组(以及一些附加信息,例如 Controller 中无法包含的业务逻辑)。
所以这是一个炉排 small, readable MFC sample .但它的背景是立体图像;我想知道如何创建一个透明的 PNG 作为应用程序背景? 最佳答案 有两种基本方法可以改变窗口的非客户端(
我想了解如何在 Laravel 5.2 中使用“胖模型,瘦 Controller ”。基本上,我主要理解为什么,是什么,但不知道怎么做。我在谷歌上搜索了一段时间,发现有几页描述了为什么(有些页面描述了
我试图理解模型上的“胖”与 Controller 上的“瘦”的概念,从我一直在讨论的内容中,我有以下示例(这来自 freenode 讨论): 问:在 MVC 范例中,它说 Fat models,ski
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
我知道之前已经回答过类似的问题 - 例如: 逻辑应该往哪里走 在哪里做某些任务等。 但我有一个更具体的问题 - 我应该在多大程度上采用这个公理:让你的 Controller 瘦,让你的模型胖! 这是一
我是一名优秀的程序员,十分优秀!