2017-10-17 101 views
0

我的應用程序管理一些參數,並允許用戶生成ETL所需的令牌以啓動批處理作業。登錄的用戶始終將其當前令牌顯示在頁面右上角,並且可以對其進行更新。然後他可以將標記複製/粘貼到ETL參數文件並啓動作業。如何在Ruby on Rails 5中指定自定義修補程序路徑?

爲了實現這一點,我插在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/route S):

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"}

我可能誤解了一些路由機制... 謝謝您的幫助!

回答

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的文檔這些例子。

相關問題