运维开发网
广告位招商联系QQ:123077622
 
广告位招商联系QQ:123077622

ruby-on-rails – 如何使重定向包括名称而不是ID?

运维开发网 https://www.qedev.com 2020-08-13 10:55 出处:网络 作者:运维开发网整理
我正在使用Rails 5.1.在我的控制器中,我想重定向到我的“show”方法 redirect_to(@organization) 但我希望URL显示为 /organization/organization_name 代替 /organization/primary_key_id 我该如何设置?我的组织模型中已经有一个字段“name”. 编辑:根据要求,这是我的PagesController的索
我正在使用Rails 5.1.在我的控制器中,我想重定向到我的“show”方法

redirect_to(@organization)

但我希望URL显示为

/organization/organization_name

代替

/organization/primary_key_id

我该如何设置?我的组织模型中已经有一个字段“name”.

编辑:根据要求,这是我的PagesController的索引方法…

class PagesController < ApplicationController

  # Should be the home page
  def index
    worker_id = params[:worker_id]
    worker = Worker.find_by_id(worker_id)
    if worker && worker.organization
      redirect_to(worker.organization)
    else
      render :file => "#{Rails.root}/public/404", layout: false, status: 404
    end
  end

end

编辑:我的config / routes.rb文件

resources :organizations, :only => [:show] do
    post :update_work
    get :get_work
    get :mine
    get :poll
    post :submit
    get :home
    get :terms_of_use
  end

这是app / model / stratum_worker.rb文件

class StratumWorker < ApplicationRecord

  has_one :organization_worker
  has_one :organization, :through => :organization_worker
我刚刚写了一篇文章 here,详细说明了这一点.我的大部分答案都是从那里开始的.相关的Rails文档是 here.

快速定义:

> Slug:用于标识记录的URL的一部分,在您的案例中为organization_name

>主键:数据库记录的唯一标识符.这通常是并且应该是id.

摘要

如果键入organization_path(@organization),它将自动使用URL中的id属性.要调整为使用organization_name,您需要进行2次更改:

>覆盖routes.rb文件中的路径参数.

>覆盖模型中的to_param方法

1.覆盖路线参数

目前,如果您运行rails路线,您的路线如下:

organizations       GET    /organizations(.:format)                     organizations#index
                    POST   /organizations(.:format)                     organizations#create
new_organization    GET    /organizations/new(.:format)                 organizations#new
edit_organization   GET    /organizations/:id/edit(.:format)            organizations#edit
organization        GET    /organizations/:id(.:format)                 organizations#show
                    PATCH  /organizations/:id(.:format)                 organizations#update
                    PUT    /organizations/:id(.:format)                 organizations#update
                    DELETE /organizations/:id(.:format)                 organizations#destroy

edit_organization和组织路径使用id作为查询组织的参数.

用它来覆盖路线参数

Rails.application.routes.draw do
  resources :organizations, param: :organization_name
end

现在rails路由将显示您的路由如下所示:

organizations       GET    /organizations(.:format)                                    organizations#index
                    POST   /organizations(.:format)                                    organizations#create
new_organization    GET    /organizations/new(.:format)                                organizations#new
edit_organization   GET    /organizations/:organization_name/edit(.:format)            organizations#edit
organization        GET    /organizations/:organization_name(.:format)                 organizations#show
                    PATCH  /organizations/:organization_name(.:format)                 organizations#update
                    PUT    /organizations/:organization_name(.:format)                 organizations#update
                    DELETE /organizations/:organization_name(.:format)                 organizations#destroy

2.覆盖模型参数

默认情况下,organization.to_param将返回组织的ID.这需要被覆盖,通过修改模型来执行此操作:

class Organization < ApplicationRecord
  def to_param
    organization_name
  end
end

结论&警告

您现在可以像往常一样继续使用重定向和表单,但现在使用组织名称而不是使用id的路由.

还有,祝你的采矿池好运! Lemme知道你正在挖掘哪枚硬币,我可能会加入!

此外,我没有介绍这个,因为它不是原始问题的一部分,但是,您应该确保organization_name是唯一的!您不仅应该在模式中添加唯一性约束验证:organization_name,uniqueness:true,还应该在迁移中在数据库级别强制执行它.

附录1:定制路线

当您的路线定义如下:

resources :organizations, :only => [:show] do
    post  'update_work'
    get   'get_work'
    get   'mine'
    get   'poll'
    post  'submit'
    get   'home'
    get   'terms_of_use'
  end

您的路线将如下:

organization_update_work POST   /organizations/:organization_id/update_work(.:format)                                    organizations#update_work
    organization_get_work GET    /organizations/:organization_id/get_work(.:format)                                       organizations#get_work
        organization_mine GET    /organizations/:organization_id/mine(.:format)                                           organizations#mine
        organization_poll GET    /organizations/:organization_id/poll(.:format)                                           organizations#poll
      organization_submit POST   /organizations/:organization_id/submit(.:format)                                         organizations#submit
        organization_home GET    /organizations/:organization_id/home(.:format)                                           organizations#home
organization_terms_of_use GET    /organizations/:organization_id/terms_of_use(.:format)                                   organizations#terms_of_use
             organization GET    /organizations/:id(.:format)                                                             organizations#show

像这样改变参数:

resources :organizations, :only => [:show], param: :organization_name do
    post  'update_work'
    get   'get_work'
    get   'mine'
    get   'poll'
    post  'submit'
    get   'home'
    get   'terms_of_use'
  end

将您的路线改为

organization_update_work POST   /organizations/:organization_organization_name/update_work(.:format)                     organizations#update_work
    organization_get_work GET    /organizations/:organization_organization_name/get_work(.:format)                        organizations#get_work
        organization_mine GET    /organizations/:organization_organization_name/mine(.:format)                            organizations#mine
        organization_poll GET    /organizations/:organization_organization_name/poll(.:format)                            organizations#poll
      organization_submit POST   /organizations/:organization_organization_name/submit(.:format)                          organizations#submit
        organization_home GET    /organizations/:organization_organization_name/home(.:format)                            organizations#home
organization_terms_of_use GET    /organizations/:organization_organization_name/terms_of_use(.:format)                    organizations#terms_of_use
             organization GET    /organizations/:organization_name(.:format)                                              organizations#show

哪个应该与您的重定向完全一致.

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消