如何在 Rails 5 上的 Ruby 中指定自定义 Patch 路由?

How to specify a custom Patch route in Ruby on Rails 5?

我的应用程序管理一些参数并允许用户生成 ETL 启动批处理作业所需的令牌。登录用户始终在页面右上角显示他当前的令牌,他可以更新它。然后他可以copy/paste将token添加到ETL参数文件并开始作业。

为此,我在 application.html.rb 中插入了以下内容:

            <nav class="top_menu">
                <ul>
                    <% if user_signed_in? %>
                    <li> <%= t('User') %>: <%= link_to (current_user.first_name + ' ' + current_user.name), edit_user_registration_path %></li>
                    <li> | <%= t('Token') %>: <%= current_user.api_token %> </li>
                    <li> | <%= link_to t('Sign_out'), destroy_user_session_path, method: "delete" %></li>
                    <% else %>
                    <li> <%= link_to t('Sign_in'), new_user_session_path %></li>
                    <% end %>
                    <li> | <%= link_to t('Help'), help_path("help-index") %> </li>
                </ul>
                    <% if user_signed_in? %>
                        <!-- token generation form -->
                        <%= form_for current_user, :url => {:controller =>"users_controller", :action => "set_token", :method => "patch"} do |f| %>
                                    <% if current_user.errors.any? %>
                                        <div id="error_explanation">
                                            <h2><%= pluralize(current_user.errors.count, "error") %> prohibited this user from being saved:</h2>

                                            <ul>
                                            <% current_user.errors.full_messages.each do |message| %>
                                                <li><%= message %></li>
                                            <% end %>
                                            </ul>
                                        </div>
                                    <% end %>
                            <ul>
                                <li><%= t('Count') %>: <%= f.text_field :api_token_count, :size => "4" %> </li>
                                <li><%= t('Validity') %>: <%=  f.text_field :api_token_validity, :size => "10" %> </li>
                                <li class="actions" ><%= f.submit "Renew" %></li>
                            </ul>
                        <% end %>
                    <% end %>
            </nav>

用户控制器包含此功能:

  def set_token
  @user.updated_by = current_user.user_name
  @user.api_token = (BCrypt::Password.create(current_user.user_name+Time.now.to_i.to_s))

  respond_to do |format|
    if @user.update(user_params)
      format.html { redirect_to @user, notice: 'Token was successfully renewed.' }
      format.json { render :show, status: :ok, location: @user }
    else
      format.html { render :edit }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

并且 routes.rb 文件包含这个额外的成员路由:

devise_for :users

resources :users, :only=>[:edit, :update, :show, :index, :set_token] do
  patch 'set_token', on: :member
end

似乎由 Rails (/rails/info/routes) 正确生成:

set_token_user_path PATCH /users/:id/set_token(.:format) users#set_token

Rails ActionController 发出 UrlGenerationError:

No route matches {:action=>"set_token", :controller=>"users_controller", :method=>"patch"}

我可能误解了路由机制中的某些东西...... 谢谢您的帮助!

我觉得你的语法有点不对劲。尝试:

form_for(current_user, url: set_token_user_path(current_user), html: {method: "patch"})

或者:

form_for(current_user, url: set_token_user_path(current_user), method: :patch)

我在 Rails 文档中找到了这两个示例。