如何在Ruby on Rails 5中指定自定义补丁路由?

时间:2017-10-17 02:35:31

标签: ruby-on-rails url-routing

我的应用程序管理一些参数,并允许用户生成ETL启动批处理作业所需的令牌。登录用户始终在页面右上角显示当前令牌,并且可以续订。然后他可以将令牌复制/粘贴到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)   用户#set_token

Rails ActionController发出UrlGenerationError:

  

没有路线匹配{:action =&gt;&#34; set_token&#34;,   :controller =&gt;&#34; users_controller&#34;,:method =&gt;&#34; patch&#34;}

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

1 个答案:

答案 0 :(得分:0)

我认为你的语法略有偏差。尝试:

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文档中找到了这两个例子。