本文介绍了如何直接创建到示波器的链接/路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 >在我的路线文件末尾,例如:

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_pa​​th,corroborated_pa​​th,unconfirmed_pa​​th 。

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_pa​​th,corroborated_pa​​th ,根据 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

这篇关于如何直接创建到示波器的链接/路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:56