简体   繁体   中英

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

My application manages some parameters and allows a user to generate a token required by an ETL to start a batch job. A logged in user always have his current token displayed on the top right of the page, and he can renew it. Then he can copy/paste the token to the ETL parameter file and start the job.

To achieve this, I inserted the following in the 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>

The users controller includes this function:

  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

And the routes.rb file contains this additional member route:

devise_for :users

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

Which seems correctly generated by Rails (/rails/info/routes):

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

Rails ActionController issues a UrlGenerationError:

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

I probably misunderstood something in the routing mechanism... Thank you for your help!

I think your syntax is slightly off. Try:

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

Or:

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

I found both of these examples in the Rails docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM