如何直接创建到示波器的链接/路由? [英] How do I create a link/route directly to a scope?
问题描述
所以我有一个 Post
模型,它具有以下范围:
So I have a Post
model, that has these scopes:
scope :unconfirmed, -> { where( status: "unconfirmed") }
scope :corroborated, -> { where( status: "corroborated") }
scope :confirmed, -> { where( status: "confirmed") }
我想做的是某人去帖子/已确认
它显示了所有范围已确认的帖子。
What I want to do is when someone goes to posts/confirmed
it shows all the posts scoped to confirmed.
最优雅的方法是什么?另外,如何在 routes.rb
中为每个作用域创建路径?
What is the most elegant way to do this? Also, how do I create a path for each scope in my routes.rb
?
编辑1
我知道一种方法是简单地在我的帖子控制器中为每个可枚举范围创建一个动作(即 unconfirmed
等。)
I know one way to do this is to simply create an action in my posts controller for each enumerable scope (i.e. unconfirmed
, etc.).
但是我宁愿在索引操作中完成所有这些操作-因此我正在渲染并使用索引视图。
But I would rather just do all of this in my index action - so I am rendering and using my Index view.
我尝试在 Post#Index
中执行此操作:
I have tried doing this in my Post#Index
:
if params[:unconfirmed]
@posts = Post.published.unconfirmed
end
但这只是给我一个路由错误。
But that just gives me a routing error.
这是错误:
Started GET "/unconfirmed" for 127.0.0.1 at 2014-10-27 02:29:51 -0500
Processing by PostsController#show as HTML
Parameters: {"id"=>"unconfirmed"}
Post Load (3.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."publication_status" = 1 AND "posts"."slug" = 'unconfirmed' ORDER BY "posts"."id" ASC LIMIT 1
Post Load (1.7ms) SELECT "posts".* FROM "posts" INNER JOIN "friendly_id_slugs" ON "friendly_id_slugs"."sluggable_id" = "posts"."id" AND "friendly_id_slugs"."sluggable_type" = 'Post' WHERE "posts"."publication_status" = 1 AND ("friendly_id_slugs"."sluggable_type" = 'Post' AND "friendly_id_slugs"."slug" = 'unconfirmed') ORDER BY "posts"."id" ASC LIMIT 1
Completed 404 Not Found in 28ms
ActiveRecord::RecordNotFound - ActiveRecord::RecordNotFound:
friendly_id (5.0.4) lib/friendly_id/finder_methods.rb:23:in `find'
() myapp/controllers/posts_controller.rb:78:in `set_post'
set_post
方法是:
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.published.find(params[:id])
end
我的URL使用 friendly_id
。
这是我的 routes.rb
:
get 'posts/:id' => redirect("/%{id}")
get '/:friendly_id', to: 'posts#show'
get 'posts/:friendly_id', to: 'posts#show'
编辑2
我仍然收到此错误。我认为是这样,因为每当我尝试执行 / confirmed
或其他任何状态时,这就是我的日志:
I am still getting this error. I believe it to be the case because whenever I try to do /confirmed
or any of the other statuses, this is what my log looks like:
Started GET "/confirmed" for 127.0.0.1 at 2014-10-31 18:28:55 -0500
ActiveRecord::SchemaMigration Load (2.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by PostsController#show as HTML
Parameters: {"friendly_id"=>"confirmed"}
Completed 404 Not Found in 58ms
遵循以下路由规则:
get '/:friendly_id', to: 'posts#show'
get 'posts/:friendly_id', to: 'posts#show'
即使我为我的资源帖子放置了推荐的路由规则,它仍然会这样做 >在我的路线文件末尾,例如:
It still does this even if I put the recommended routing rules for my resources post
at the end of my routes file, i.e. like this:
resources :posts do
collection do
get 'confirmed' => 'posts#index', status: 'confirmed'
get 'unconfirmed' => 'posts#index', status: 'unconfirmed'
get 'corroborated' => 'posts#index', status: 'corroborated'
end
end
root to: "posts#index"
如何使状态路由不重定向到 posts#index
操作?
How do I get the status routes to not be redirected to the posts#index
action?
编辑3
查看我完整的 routes.rb
Rails.application.routes.draw do
%w[privacy terms].each do |page|
get page, controller: 'info', action: page
end
resources :locations
devise_for :users, :path_names => { :sign_up => "register",
:sign_in => "login",
:sign_out => "logout",
:settings => "settings" },
:controllers => { :confirmations => "confirmations" }
devise_scope :user do
get "login", :to => "devise/sessions#new"
get "register", :to => "devise/registrations#new"
get "settings", :to => "devise/registrations#edit"
delete "logout", :to => "devise/sessions#destroy"
end
resources :posts do
collection do
get 'confirmed' => 'posts#status', status: 'confirmed'
get 'unconfirmed' => 'posts#status', status: 'unconfirmed'
get 'corroborated' => 'posts#status', status: 'corroborated'
end
end
get 'posts/:id' => redirect("/%{id}")
get '/:friendly_id', to: 'posts#show'
get 'posts/:friendly_id', to: 'posts#show'
# This supports legacy URLs e.g:
# http://www.example.com/rbt/26766-algaj-pays-tribute-to-the-honourable-roger-clarke.html
get '/rbt/:name', to: redirect {|path_params, _| "/#{path_params[:name].gsub(/^\d+\-/, '')}" }
get ':name', to: 'posts#show'
root to: "posts#index"
end
编辑4
难题的最后一部分是问题的原始前提,即如何创建指向该新创建路径的链接。
The last bit of the puzzle is the original premise of the question, how do I create a link to that newly created path.
棘手的是,我想根据状态动态创建链接路径。
The tricky bit is that I want dynamically create the link_path based on the status.
例如,新路线规则的结果,我现在有 confirmed_path,corroborated_path,unconfirmed_path
。
For instance, as a result of the new routes rules, I now have confirmed_path, corroborated_path, unconfirmed_path
.
但是,我正在显示它们在我看来是这样的:
However, I am displaying them in my view like so:
<span class="post-status status label<%=render partial: "shared/color", locals: {post: post.status }%>"><%= post.status.try(:upcase) %></span>
我尝试这样做:
<span class="post-status status label<%=render partial: "shared/color", locals: {post: post.status }%>"><%= link_to post.status.try(:upcase), post.status.path %></span>
但这给了我未定义的方法路径
错误。
在正常情况下,我只是简单地进行字符串插值,而Ruby会渲染当前字符串。鉴于Ruby应该解析此路径,因此我怀疑它是否行得通。
In a normal instance, I would simply do string interpolation and Ruby would render the current string. Given that Ruby is supposed to parse this path, I doubt that would work.
因此,如何获取该视图以自动生成 confirmed_path,corroborated_path ,根据
post.status
是什么动态地确定路径吗?
So how do I get that view to automagically generate confirmed_path, corroborated_path, unconfirmed_path
dynamically based on what post.status
is?
推荐答案
您看到的是迷路。在这里调用 posts / confirmed
将转到 posts#show
,而不是您的 posts#index
方法代替。原因是操作包优先考虑从上到下的路由,并且由于您的路由是这样定义的:
As you see you are lost in routes. Here calling posts/confirmed
will go at posts#show
instead of your posts#index
method instead. The reason is action pack give priority of routes from top to bottom, and since your routes are defined like this:
resources 'posts'
get 'posts/:id' => redirect("/%{id}")
get '/:friendly_id', to: 'posts#show'
get 'posts/:friendly_id', to: 'posts#show'
所有帖子/未确认
, posts / confirmed
等永远不会使用index方法,因为它将与 posts#show
然后与获取'posts /:friendly_id',以:'posts#show'
。
All posts/unconfirmed
, posts/confirmed
, etc will never make to the index method as it will be matched with posts#show
, and then with get 'posts/:friendly_id', to: 'posts#show'
.
现在,即使您将所有 get
路由到顶部,它将永远不会使用您以后要定义的其他方法,因为所有帖子/未确认
, posts / confirmed
等将与 get'posts /:id'=>匹配。 redirect( /%{id})
并将被重定向为 /已确认
, /未确认
等。
Now, even if you move your all get
routes to top, it will never make it to other methods you might wanna define later, as all posts/unconfirmed
, posts/confirmed
, etc will be matched with get 'posts/:id' => redirect("/%{id}")
and will be redirected as /confirmed
, /unconfirmed
, etc.
推荐的方法是为各个路线使用单独的方法,因为将来您可能还希望确认的帖子与证实的帖子具有不同的行为。为此,您的路线将如下所示:
Recommended way is to have separate methods for individual routes, since in future you also might want to have a different behavior for confirmed posts than corroborated posts. For that your routes will look like this:
resources :posts do
collection do
get 'confirmed'
get 'unconfirmed'
get 'corroborated'
end
end
运行 $耙路| grep帖子
会给出:
confirmed_posts GET /posts/confirmed(.:format) posts#confirmed
unconfirmed_posts GET /posts/unconfirmed(.:format) posts#unconfirmed
corroborated_posts GET /posts/corroborated(.:format) posts#corroborated
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
,然后创建单独的方法,即已确认
,未确认
,已确认
等。
and then you create separate methods, i.e. confirmed
, unconfirmed
, corroborated
etc.
但是,您始终可以将所有这些路线都指向这样的路线(
However, you can always point all these routes to one like this(which is not recommended):
resources :posts do
collection do
get 'confirmed' => 'posts#status', status: 'confirmed'
get 'unconfirmed' => 'posts#status', status: 'unconfirmed'
get 'corroborated' => 'posts#status', status: 'corroborated'
end
end
然后在您的 PostsController
中:
def status
@posts = Post.published.where(status: params[:status])
end
更新:将您的route.rb更改为-
UPDATE: Change your routes.rb to this -
Rails.application.routes.draw do
%w[privacy terms].each do |page|
get page, controller: 'info', action: page
end
resources :locations
devise_for :users, :path_names => { :sign_up => "register",
:sign_in => "login",
:sign_out => "logout",
:settings => "settings" },
:controllers => { :confirmations => "confirmations" }
devise_scope :user do
get "login", :to => "devise/sessions#new"
get "register", :to => "devise/registrations#new"
get "settings", :to => "devise/registrations#edit"
delete "logout", :to => "devise/sessions#destroy"
end
resources :posts
get '/confirmed' => 'posts#status', status: 'confirmed', as: :confirmed
get '/unconfirmed' => 'posts#status', status: 'unconfirmed', as: :unconfirmed
get '/corroborated' => 'posts#status', status: 'corroborated', as: :corroborated
get 'posts/:id' => redirect("/%{id}")
get '/:friendly_id', to: 'posts#show'
get 'posts/:friendly_id', to: 'posts#show'
# This supports legacy URLs e.g:
# http://www.example.com/rbt/26766-algaj-pays-tribute-to-the-honourable-roger-clarke.html
get '/rbt/:name', to: redirect {|path_params, _| "/#{path_params[:name].gsub(/^\d+\-/, '')}" }
get ':name', to: 'posts#show'
root to: "posts#index"
end
确保您的 PostsController
中具有 status
方法:
def status
# your code for fetching posts goes here!
render text: params[:status] # this is to show an example that `status` is set properly
end
这篇关于如何直接创建到示波器的链接/路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!