Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot load custom controller action from link_to
    text
    copied!<p>I'm a rails beginner and have been several hours now trying to figure out why my "generate new password" link, which was supposed to execute my custom update_password action inside my users controller, and then flash the password back on the screen (later i plan to send this by e-mail or sms, but for now this would do) keeps executing the create method/action... I understand I have two POST methods for the users index screen...but cannot understand (since i even placed it first on the routes file) why create is the one that keeps getting executed. I know it doesn't go to the update method, because i filled it with debugger logging messages, which do not show up anywhere. (and the logging is active since i see the index method logging message)</p> <p>Here is what I'm doing : </p> <p>routes.rb file extract :</p> <pre><code>match 'users?user_id=(:user_id)', to: "users#update_password", via: :post, as: "users_update_password" </code></pre> <p>resources :users</p> <p>rake routes (controller users)</p> <pre><code>users_update_password POST /users?user_id=(:user_id)(.:format) users#update_password users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy signup /signup(.:format) users#new </code></pre> <p>users_controller.rb</p> <pre><code> def update_password logger.info "Inside update_password" logger.flush password= generate_password() logger.debug "Password is #{password}" @user = User.find_by_id(params[:user_id]) logger.debug "user is #{@user}" if @user.update_attributes(password:password, password_confirmation:password) logger.info "inside the if" flash[:notice] = "New password for user #{@user.name}: #{password}" logger.debug "Flash is #{flash}" redirect_to users_path else logger.debug "I am on else of update_password" flash[:alert] = "Name: #{@user.name} password: #{password}" render 'index' end </code></pre> <p>end</p> <pre><code>def index logger.info "Inside controller index method" @users = User.paginate(page: params[:page]) </code></pre> <p>end</p> <p>_user.html.erb</p> <pre><code>&lt;% if current_user.admin? &amp;&amp; !current_user?(user) %&gt; | &lt;%= link_to "delete", user, method: :delete, confirm: "Are you sure?" %&gt; | &lt;%= link_to "Generate new password", users_update_password_path(:user_id=&gt;user.id), method: :post, confirm: "Are you sure? This will reset current password" %&gt; &lt;% end %&gt; </code></pre> <p>Thank you for your help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload