Updating a record with rails 4 - ruby-on-rails

I'm trying to update a record with an admin user with a "validate" button that changes the record's status from pending to confirmed. I've created a form, and the route to do so, however the controller is giving me trouble, I'm not sure what to code for it to update the specific record i click validate for.
Hours controller
def index
#allhours = HourLog.where(status:'Pending')
end
def update
#hour = HourLog.where(status:'Pending')
#hour.update(#hour.id, :status)
end
Hours/index.html.erb
<td><%=form_for(:hour_log, method: :put) do |f|%>
<%= f.hidden_field :status, :value => 'Confirmed' %>
<%= f.submit 'Validate'%>
<%end%>
</td>
Any help would be fantastic, thanks!
error:
NoMethodError in HoursController#update
undefined method `id' for #
I know something is wrond with the (#hour.id) section of the controller in the update def, but I don't know what to replace it with
edit: rake routes
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
signup GET /signup(.:format) users#new
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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
GET /users(.:format) users#index
PUT /users(.:format) users#update
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
dashboard GET /dashboard(.:format) hours#new
dashboard_hours GET /dashboard/hours(.:format) hours#index
hours PUT /hours(.:format) hours#update
GET /hours(.:format) hours#index
POST /hours(.:format) hours#create
new_hour GET /hours/new(.:format) hours#new
edit_hour GET /hours/:id/edit(.:format) hours#edit
hour GET /hours/:id(.:format) hours#show
PATCH /hours/:id(.:format) hours#update
PUT /hours/:id(.:format) hours#update
DELETE /hours/:id(.:format) hours#destroy
hours Controller
class HoursController < ApplicationController
before_action :require_user
before_action :require_admin, only: [:index]
def index
#allhours = HourLog.where(status:'pending')
end
def new
#hour = current_user.hour_logs.new
#entry = current_user.hour_logs.all
end
def edit
flash[:warning] = "Hash: #{params}"
end
def create
#hour = HourLog.new(hour_params)
#hour.user_id = current_user.id if current_user
#hour.status = 'pending'
if #hour.save
redirect_to '/dashboard'
end
end
private
def hour_params
params.require(:hour_log).permit(:assignment, :hours, :supervisor, :date)
end
def update_hour_params
params.require(:hour_log).permit(:status)
end
end
HourLog method
class HourLog < ActiveRecord::Base
belongs_to :user
end

I see this has an answer accepted, but I'd like to propose an alternative solution for anyone who may come along and find this question. Assumptions: The base page is an index with all pending items and the click is a simple state change. (A state toggle could be easily added to this solution.)
(I'm using the naming from the question.)
Using link_to as a message
A state change from 'Pending' to 'Confirmed' for a flag could be implemented by using the link_to to trigger the controller action to update this flag. Hash params may be passed via a link_to enabling simplified logic in the controller.
VIEW: Index.html.erb
<% #allhours.each do |hour| %>
<ul>
<li><%= hour.textdescriptionvar %> is an item pending approval.
<%= link_to(" Click to approve", edit_hour_path(hour, :status => "Confirmed"))
</li>
</ul>
<% end %>
CONTROLLER: HourLogsController
def edit
flash[:info] = "Hash: #{params}"
#hour = HourLog.find(params[:id])
end
def update
#hour = HourLog.find(params[:id])
#hour.update_attributes(hours_params)
if #hour.save
redirect_to #hour, :notice => "Successfully changed stat."
else
render 'index' # Any action here, index for example
end
end
private
def hours_params
params.require(:hour_log).permit(:status)
end
I would recommend a data: confirm message be added to the link_to,but I wanted to mimic the poster's situation as posted.
Rails spec for link_to here.

#hour = HourLog.where(status:'Pending') will return an ActiveRecord Relation, not a single record.
Try this:
#hour = HourLog.find_by(status: 'pending')
#hour.update_attribute :status, params[:hour_log][:status]
This method finds the first record with status ="pending" and updates that instead of the specific record.

You're going to want #hour.update params[:hour_log] instead because update_attribute doesn't run validations. and keep data in your database lowercase unless it's something written by a user. this link is useful http://www.davidverhasselt.com/set-attributes-in-activerecord/

Related

Rails Create 302 Error

I am working on a toy web app to practice Ruby on Rails, and I've run into an issue that I haven't been able to figure out in the documentation. Essentially, I have a form to create tasks and relate them to users, but the form is issuing a 302 error when submitted and the record is not being saved.
I have two models, User and Task
User
class User < ApplicationRecord
has_many :tasks
end
Task
class Task < ApplicationRecord
belongs_to :article
end
The idea is simple, a user can have many tasks. The migrations for these are working fine.
I have created a (partial) form for creating tasks:
_compact_form.html.erb
<%= form_for( [#currentUser, #currentUser.tasks.build] ) do |f| %>
<p>
<%= f.label :label %>
<%= f.text_field :label %>
<%= f.label :complete %>
<%= f.check_box :complete%>
<%= f.submit %>
</p>
<% end %>
and this is attached to a dashboard view, which is trivially wired up to a dashboard controller:
class DashboardController < ApplicationController
def index
#currentUser = User.find(1)
end
end
All pretty straightforward so far. My tasks controller has a few methods to manage creation so far.
Tasks controller:
class TasksController < ApplicationController
def new
end
def show
#user = User.find( params[:user_id] )
end
def create
#user = User.find( params[:user_id] )
#task = #user.tasks.create( task_params )
redirect_to '/'
end
private
def task_params
params.require(:task).permit(:label, :complete)
end
end
And now we're at the point where I get confused. I've manually inserted a task into my database and I'm able to retrieve it just fine, but for some reason, my create route is giving an HTTP 302 error. I have set up my routes like so:
resources :users do
resource :tasks
end
root 'dashboard#index'
get 'dashboard/index'
and rake routes outputs
Prefix Verb URI Pattern Controller#Action
new_user_tasks GET /users/:user_id/tasks/new(.:format) tasks#new
edit_user_tasks GET /users/:user_id/tasks/edit(.:format) tasks#edit
user_tasks GET /users/:user_id/tasks(.:format) tasks#show
PATCH /users/:user_id/tasks(.:format) tasks#update
PUT /users/:user_id/tasks(.:format) tasks#update
DELETE /users/:user_id/tasks(.:format) tasks#destroy
POST /users/:user_id/tasks(.:format) tasks#create
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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / dashboard#index
dashboard_index GET /dashboard/index(.:format) dashboard#index
so the create route appears to be there, and it appears to be correct.
Now for the strangest part! If I use my form, this is what I see in the console:
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.1ms) begin transaction
(0.1ms) commit transaction
This appears to be the correct code in my tasks#create method, so I'm pretty confused where the data is going. Can anyone shed some light into next steps for debugging this? I have spent some time creating different forms and working through the examples in this guide, but I seem to have made a mistake somewhere along the lines and I can't find it.
You should declare your redirection inside an respond_to block, redirects without format declaration cause 302 status responses because it doesn't know which kind of response to give for redirection, so it assumes your client would process the location header field that goes with the response, you should do something like this:
respond_to do |format|
format.html { redirect_to '/' }
end
302 status isn't an error, it is an indication of redirection and the redirected location is being sent as HTTP header. As far as model not being saved, you should provide better logs results, it could be a vast number of issues, like unpermitted params, validations, database error...

Ruby on Rails: Adding Additional RESTFUL Actions

In my rails app I would like to have two patch methods for updating the users profile.
First, I will have an 'account_settings' GET request, which will use the standard edit/update REST action to update certain parameters. Then, I would like to have an additional 'edit_profile' and 'update_profile' actions to get a page that will allow the user to update different user attributes. Here's how it looks in my users_controller.rb for a better idea:
#For account settings page
#This is for the account settings page
#(only changing email and password)
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(account_settings_params)
flash.now[:success] = "Your settings have been successfully updated."
format.html {redirect_to #user}
else
format.html {redirect_to edit_user_path}
flash[:error] = "Please be sure to fill out your email, password, and password confirmation."
end
end
end
def edit_profile
#user = User.find(params[:id])
end
#For update profile
def update_profile
#user = User.find(params[:id])
respond_to do |format|
if #user.update_attributes(user_profile_params)
flash.now[:success] = "Your profile has been updated."
format.html {redirect_to #user}
else
format.html {redirect_to edit_profile_user_path}
flash[:error] = "Please be sure to fill out all the required profile form fields."
end
end
end
private
def account_settings_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
def user_profile_params
params.require(:user).permit(:name, :about_me, :location, :image_url)
end
Selection from my current routes.rb :
#Account Settings Just for Email and Password
get 'account_settings' => 'users#edit'
patch 'settings' => 'users#update'
resources :users do
member do
get :edit_profile
put :update_profile
end
end
Results of rake routes:
edit_profile_user GET /users/:id/edit_profile(.:format) users#edit_profile
update_profile_user PATCH /users/:id/update_profile(.:format) users#update_profile
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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
My navbar partial:
-if logged_in?
-# Logged in links
%li
=link_to 'Logout', logout_path, method: :delete
%li
=link_to 'Account Settings',edit_user_path(#current_user)
%li
=link_to 'Edit My Profile', edit_profile_user_path(#current_user)
%li
=link_to 'View My Profile', user_path(#current_user)
%li
=link_to 'Members', users_path
On the edit_profile page, my form looks like this:
=form_for #user, path: update_profile_user_path(#user) do |f|
With my current implementation, visiting the edit_profile page and posting the form will lead back to the regular edit page with my rails server saying that the parameters were unpermitted. However, as you can see in my update_profile method in my controller, the controller method for update_profile accepts user_profile_params rather than the account_settings_params . Any insight onto why it might be doing this?
A few notes:
You don't need render "edit_profile" because that is done by default
You don't need to overwrite the edit route
I'd strongly suggest actually having a separate Profile controller, instead of trying to hack it in as extra actions on user.
That being said, the routes look like
resources :users do
member do
get :edit_profile
patch :update_profile
end
end
then your link would be to users/1/edit_profile (link_to "edit", edit_profile_path(#user)) and the form would be to <%= form_for #user, path: update_user_path(#user) %>
To add RESTful routes to the current resources you can use collection or members based on the requirement.
collection is used as a non membered resources like index action which gives a collection of objects where as show action needs an object to show. Hence member is used to get the action of a single object.
Here you can use
resources users do
resources member do
get :edit_profile
put :update_profile
end
end
You can also use
resources :users do
get :edit_profile, on: :member
put :update_profile, on: :member
end

Error concerning url parameter while testing view

Running the provided spec fails, and I think it might be related to the url parameters, being territories/:id/.
Error received :
1) territories/edit.html.erb shows a form with the data already in it
Failure/Error: <%= form_for(territory, remote: true) do |territory_form| %>
ActionView::Template::Error:
No route matches {:action=>"show", :controller=>"territories", :format=>nil, :id=>nil, :locale=>#<Territory id: 2, name: "Territory 4", lent_on: "2015-12-21", returned_on: "2015-12-21", lent_to: 6, created_at: "2015-12-23 10:04:15", updated_at: "2015-12-23 10:04:15">} missing required keys: [:id]
I'm working with form_for. I find it a bit weird that I receive an error about the url parameter in my view, while I'm not having anything directly related to it in the view code.
I've put the controller code as wel, just for clarity.
Can someone guide me how to either fix this or how to properly test this
edit.html.erb
<h1>Territories#edit</h1>
<p>Find me in app/views/territories/edit.html.erb</p>
<%= render partial: "territory_form", locals: { territory: #territory } %>
edit.html.erb_spec.rb
require 'rails_helper'
RSpec.describe "territories/edit.html.erb", type: :view, territories:true do
before(:each) do
#territory = create(:territory)
render
end
it "shows a form with the data already in it" do
puts "Body: #{rendered}"
expect(rendered).to have_css("label", :text => "Name")
expect(rendered).to have_css "input[value*='#{#territory.name}']"
end
end
routes.rb
Prefix Verb URI Pattern Controller#Action
sessions_new GET (/:locale)/sessions/new(.:format) sessions#new {:locale=>/en|nl/}
sign_up_new GET (/:locale)/sign_up/new(.:format) sign_up#new {:locale=>/en|nl/}
users GET (/:locale)/users(.:format) users#index {:locale=>/en|nl/}
edit_user GET (/:locale)/users/:id/edit(.:format) users#edit {:locale=>/en|nl/}
user GET (/:locale)/users/:id(.:format) users#show {:locale=>/en|nl/}
PATCH (/:locale)/users/:id(.:format) users#update {:locale=>/en|nl/}
PUT (/:locale)/users/:id(.:format) users#update {:locale=>/en|nl/}
DELETE (/:locale)/users/:id(.:format) users#destroy {:locale=>/en|nl/}
register GET (/:locale)/register(.:format) users#new {:locale=>/en|nl/}
POST (/:locale)/register(.:format) users#create {:locale=>/en|nl/}
login GET (/:locale)/login(.:format) sessions#new {:locale=>/en|nl/}
POST (/:locale)/login(.:format) sessions#create {:locale=>/en|nl/}
logout DELETE (/:locale)/logout(.:format) sessions#destroy {:locale=>/en|nl/}
overview GET (/:locale)/user/overview(.:format) dashboard#index {:locale=>/en|nl/}
territories GET (/:locale)/admin/territories(.:format) territories#index {:locale=>/en|nl/}
POST (/:locale)/admin/territories(.:format) territories#create {:locale=>/en|nl/}
new_territory GET (/:locale)/admin/territories/new(.:format) territories#new {:locale=>/en|nl/}
edit_territory GET (/:locale)/admin/territories/:id/edit(.:format) territories#edit {:locale=>/en|nl/}
territory GET (/:locale)/admin/territories/:id(.:format) territories#show {:locale=>/en|nl/}
PATCH (/:locale)/admin/territories/:id(.:format) territories#update {:locale=>/en|nl/}
PUT (/:locale)/admin/territories/:id(.:format) territories#update {:locale=>/en|nl/}
DELETE (/:locale)/admin/territories/:id(.:format) territories#destroy {:locale=>/en|nl/}
GET /:locale(.:format) sessions#new
root GET / sessions#new
territories_controller.rb
class TerritoriesController < ApplicationController
def index
#territories = Territory.all.order(:name) #Must be changed to use pagination
end
def new
#territory = Territory.new
end
def import
end
def create
#territory = Territory.new(create_params)
if (!#territory.save)
render action: :create and return
end
render action :new
end
def show
end
def edit
#territory = Territory.find(edit_params)
end
def update
territory_params = update_params
#territory = Territory.find(territory_params[:id])
if (!#territory || !#territory.persisted?)
render index and return
end
#territory.update(territory_params[:territory])
end
def destroy
end
private
def create_params
params.require(:territory).permit(:name)
end
def edit_params
params.require(:id)
end
def update_params
id = params.require(:id)
territory = params.require(:territory).permit(:name)
{:id => id, :territory => territory}
end
end
territory_form.html.erb
<%= form_for(territory, remote: true) do |territory_form| %>
<p>
<%= territory_form.label :name %>
<%= territory_form.text_field :name %>
</p>
<%= territory_form.submit %>
<% end %>
Made it into a gist
def edit
#territory = Territory.find(edit_params)
end
Should just be
def edit
#territory = Territory.find(params[:id])
end

adding tenant user inside a property view

I'm trying to add a Tenant user inside a Property#show view. I have a form there like the following:
<%= form_for(#property.tenants) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="select">
<%= f.select :type, [['Landlord'],['Tenant']] %>
</div>
<%= f.submit "Add", %>
<% end %>
The Tenant model is as follows:
class Tenant < User
belongs_to :property
def type
"Tenant"
end
end
and the Property model is as follows:
class Property < ActiveRecord::Base
attr_accessible :address, :bathrooms, :bedrooms, :postcode, :price
belongs_to :branch
belongs_to :user
has_many :ownerships
has_many :landlords, :through => :ownerships
has_many :tenants
end
When I click on the Add button i'm magically redirected to the root path (/).
I'm expecting it to add a new tenant for that specific property that I'm viewing in the show view but it just redirects me to the root path.
Feel free to ask for any clarifications
results of rake routes:
tenants_index GET /tenants/index(.:format) tenants#index
tenants_show GET /tenants/show(.:format) tenants#show
tenants_new GET /tenants/new(.:format) tenants#new
landlords_new GET /landlords/new(.:format) landlords#new
tenants_edit GET /tenants/edit(.:format) tenants#edit
tenants_create GET /tenants/create(.:format) tenants#create
tenants_update GET /tenants/update(.:format) tenants#update
tenants_destroy GET /tenants/destroy(.:format) tenants#destroy
properties GET /properties(.:format) properties#index
POST /properties(.:format) properties#create
new_property GET /properties/new(.:format) properties#new
edit_property GET /properties/:id/edit(.:format) properties#edit
property GET /properties/:id(.:format) properties#show
PUT /properties/:id(.:format) properties#update
DELETE /properties/:id(.:format) properties#destroy
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
companies GET /companies(.:format) companies#index
POST /companies(.:format) companies#create
new_company GET /companies/new(.:format) companies#new
edit_company GET /companies/:id/edit(.:format) companies#edit
company GET /companies/:id(.:format) companies#show
PUT /companies/:id(.:format) companies#update
DELETE /companies/:id(.:format) companies#destroy
branches GET /branches(.:format) branches#index
POST /branches(.:format) branches#create
new_branch GET /branches/new(.:format) branches#new
edit_branch GET /branches/:id/edit(.:format) branches#edit
branch GET /branches/:id(.:format) branches#show
PUT /branches/:id(.:format) branches#update
DELETE /branches/:id(.:format) branches#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
agents GET /agents(.:format) users#index
POST /agents(.:format) users#create
new_agent GET /agents/new(.:format) users#new
edit_agent GET /agents/:id/edit(.:format) users#edit
agent GET /agents/:id(.:format) users#show
PUT /agents/:id(.:format) users#update
DELETE /agents/:id(.:format) users#destroy
landlords GET /landlords(.:format) users#index
POST /landlords(.:format) users#create
new_landlord GET /landlords/new(.:format) users#new
edit_landlord GET /landlords/:id/edit(.:format) users#edit
landlord GET /landlords/:id(.:format) users#show
PUT /landlords/:id(.:format) users#update
DELETE /landlords/:id(.:format) users#destroy
tenants GET /tenants(.:format) users#index
POST /tenants(.:format) users#create
new_tenant GET /tenants/new(.:format) users#new
edit_tenant GET /tenants/:id/edit(.:format) users#edit
tenant GET /tenants/:id(.:format) users#show
PUT /tenants/:id(.:format) users#update
DELETE /tenants/:id(.:format) users#destroy
root / static_pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
dashboard /dashboard(.:format) static_pages#dashboard
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
properties_controller.rb
class PropertiesController < ApplicationController
# GET /properties
# GET /properties.json
def index
#properties = Property.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #properties }
end
end
def show
#property = Property.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #property }
end
end
# GET /properties/new
# GET /properties/new.json
def new
# #property = Property.new
#property = current_user.properties.build if signed_in?
respond_to do |format|
format.html # new.html.erb
format.json { render json: #property }
end
logger.debug("hello from new")
end
# GET /properties/1/edit
def edit
#property = Property.find(params[:id])
end
# POST /properties
# POST /properties.json
def create
##property = Property.new(params[:property])
#property = current_user.branch.properties.build(params[:property]) if signed_in?
respond_to do |format|
#property.user_id = current_user.id
if #property.save
format.html { redirect_to #property, notice: 'Property was successfully created.' }
format.json { render json: #property, status: :created, location: #property }
else
format.html { render action: "new" }
format.json { render json: #property.errors, status: :unprocessable_entity }
end
end
end
end
I think there are several things that doesn't seem quite right.
Try this.
Add #tenant instance variable to the show method of Properties controller.
def show
...
#tenant = #property.tenants.build
end
And modify form_for line to this.
<%= form_for #tenant do |f| %>
In your properties_controller.rb,
def show
#tenant = Tenant.new
end
Your from should then be:
<%= form_for #tenant do |f| %>
You should also add a hidden field with the ID of the property for when you submit your form.
Then for your create action in properties_controller.rb
def create
tenant = Tenant.new(params[:tenant])
tenant.property_id = params[:property_id] #This is from that hidden field with your property_id and this also assumes you have a column in your tenant table for property_id
tenant.save
#the shorter way would just be tenant = Tenate.create(params[tenant]) but I want to emphasize the property_id since this attribute is paramount to your property-tenant relationship
end
This should effectively create a form for a new tenant and when you create the actual tenant record, you will save the property ID with the new tenant record.
On another note, I would suggest moving this to the Tenants controller per Rails convention and having the form under the new action since you are really creating a new tenant record.

Cannot load custom controller action from link_to

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)
Here is what I'm doing :
routes.rb file extract :
match 'users?user_id=(:user_id)', to: "users#update_password", via: :post, as: "users_update_password"
resources :users
rake routes (controller users)
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
users_controller.rb
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
end
def index
logger.info "Inside controller index method"
#users = User.paginate(page: params[:page])
end
_user.html.erb
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete, confirm: "Are you sure?" %>
| <%= link_to "Generate new password", users_update_password_path(:user_id=>user.id), method: :post, confirm: "Are you sure? This will reset current password" %>
<% end %>
Thank you for your help
It might simplify things if you make this a member of your resource routes?
resources :users do
member do
post 'update_password'
end
end
Then path helper probably looks like
link_to "Generate new password", update_password_user_path(user), method: :post
# POST. /users/:id/update_password
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
When you use HTTP POST, the data is not sent in the URL. Your route is looking for data in the URL, which is done via HTTP GET. So if you want to use a POST, make your route something like:
post "users/update_password" => "users#update_password", :as => "users_update_password"

Resources