【问题标题】:Edit model - token URL编辑模型 - 令牌 URL
【发布时间】:2016-02-29 11:32:18
【问题描述】:

我希望用户能够通过与默认 URL /merchants/:id/edit(.:format) 不同的方式编辑模型,默认 URL /merchants/:id/edit(.:format) 将使用令牌。该令牌是随机创建并存储在数据库中的。

我要创建的链接类似于merchants/token-124512/edit

现在我希望能够通过电子邮件将此链接发送给用户。 id 的默认链接是link_to "link", edit_merchant_path(@merchant, :only_path => false)

有token的会怎么样?另外,如何在 routes.rb 中声明这个?

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    解决方案

    首先我建议在您的邮件中使用_url 而不是_path,因为您想解析完整路径。

    尝试:

    link_to "link", edit_merchant_url(id: @merchant.token)

    为什么是 _url?

    使用_path 会得到<a href=merchants/token-124512/edit'>link</a>,但在邮件中你也想知道主机,所以你应该得到这个: <a href='hostname.com/merchants/token-124512/edit'>link</a>

    【讨论】:

      【解决方案2】:

      如果您使用的是 rails 4,您可以在路由中使用名为 'param' 的新概念,这将更改默认路由。

      您可以传递任何字段而不是您的 id。

      # config/routes.rb
      
      resources :merchants, param: :token_field
      
      # app/controllers/merchants_controller.rb
      
      @merchant = Merchant.find_by(token_field: params[:token_field])
      

      示例

      resources :merchants, param: :your_fleld
      
      
           merchants GET  /merchants(.:format)                  merchants#index
                  POST /merchants(.:format)                  merchants#create
       new_merchants GET  /merchants/new(.:format)              merchants#new
      edit_merchants GET  /merchants/:your_field/edit(.:format) merchants#edit
      
      
      Merchant.find_by(your_field: params[:your_field])

      You can find the documentation here

      【讨论】:

      • 嘿,试试这个并阅读文档。我已经详细说明了答案并显示了生成的路线。请检查。 :-)
      猜你喜欢
      • 1970-01-01
      • 2011-06-05
      • 2021-08-19
      • 2019-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      相关资源
      最近更新 更多