Ruby on rails 铁路:半宁静路线
我在下面列出了三条最下面的路线,它们非常容易出错,因为通常会声明其他路线:Ruby on rails 铁路:半宁静路线,ruby-on-rails,ruby-on-rails-3,rails-routing,Ruby On Rails,Ruby On Rails 3,Rails Routing,我在下面列出了三条最下面的路线,它们非常容易出错,因为通常会声明其他路线: # normal routes resources :documents, :except => [:show, :edit, :update] resources :photos, :except => [:show, :index] ... # error-prone routes get ":client_code" => "share#index", :as => :shares, :fo
# normal routes
resources :documents, :except => [:show, :edit, :update]
resources :photos, :except => [:show, :index]
...
# error-prone routes
get ":client_code" => "share#index", :as => :shares, :format => false
get ":client_code/:id" => "share#show", :as => :share, :format => false
get ":client_code/:document_id/more/:component_id" => "share#more", :as => :more, :format => false
我在ShareController
中有一些方法来处理这样的请求:
def show
get_user_by_parameter
if get_document_by_user_or_issue and @document.is_showable? and @parameter_user == @document.user
...
end
private
def get_user_by_parameter
@parameter_user = User.where(:client_code => params[:client_code]).first
end
def get_document_by_user_or_issue
if params[:id].match(/\D/)
@document = Document.where(:user_id => @user.id, :issue => params[:id]).first
else
@document = Document.find(params[:id])
end
end
我需要的路线是最小的,但这不仅是丑陋和不安宁,但它是非常容易出错
:客户端代码将始终是正在查看的@文档的所有者。这有点像安全检查/所有权之类的功能。但是,由于上面列出的所有原因:有没有更好的方法来写这篇文章?一定有更好的办法
谢谢。基于控制器的检查:
before_filter :find_document
def find_document
Document.find(params[:id])
end
def is_owner?(document)
redirect_to root_path if current_user.id != document.owner_id
end
这样的支票不是容易多了吗?我不知道你为什么会有一个股票控制人,所以我不想在这里冒昧
这将允许您执行以下操作:
resources :shares, only: [:index, :show]
此外:
可以重构为:
User.find_by(client_code: params[:client_code])
假设您使用的是最新的rails版本,否则:
User.find_by_client_code(params[:client_code])
让我知道这些股票的用途,我不确定我是否为您提供了完整的解决方案
干杯
编辑
如果您使用共享提供不同的视图,我建议您这样做:
在控制器内
def index
if params[:shares]
render 'shares'
end
end
除非你真的希望有不同的路线。这使您不必为文档模型的本质设置共享控制器。感谢您深思熟虑的回答<代码>共享
作为文档
的不同视图存在。代码就是这样已经有一段时间了,但不确定这是否是将用户发送到不同的文档
视图的好方法。
def index
if params[:shares]
render 'shares'
end
end