mardi 7 novembre 2017

Ruby-on-Rails 4 edit_user_path update not updating

Just started rails so any help appreciated I have set up everything for mode User, Users login files are below:

in controllers:

def change_password
@user = User.find(params[:id])
if @user.password_hash == Bcrypt::Engine.hash_secret(:password, salt)
@user.password = params([:password])

def update
@user = User.find(params[:id])
@user.update_attribute( :login, params[:login])
@user.udpate_attribute( :password, params[:password])
change_password
if(@user.save)
    flash:[:success] = "user updated"
redirect_to 'welcome#index'
end
end

views: index just has a link to show, then to edit

edit:

<h1>Profile:</h1>
<%= form_for(@user) do |f| %>
<li>
<%= f.label :id %>
<%= f.text_area :id %>
</li>
<li>
<%= f.label :password %>
<%= f.text_area :password %>
</li>
<li>
<%= link_to "Correct? -> Save", edit_user_path(update) %>

Sorry for my lack of understanding here - where do i implement the update/change_password method. Whenever I try to call it, the error thrown is undefined method for @user - i thought I would be able to call @user.update or just update?

Below is my rails routes:

users_index  GET     /users/index(.:format)    users#index
root         GET     /                         users#index
users        GET     /users(.:format)          users#index
             POST    /users(.:format)          users#create
new_user     GET     /users/new(.:format)      users#new
edit_user    GET     /users/new(.: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
users_new    get      /users/new(.:format)     users#new
             GET      /users/:id(.:format)     users#edit
             GET      /users/:id/edit(.:format)   users#update

and routes.rb

Rails.application.routes.draw do
get 'users/index'
root 'users/index'
resources :users
get 'users/new' => 'users#new'
get 'users/:id/', to: 'users#edit'
get 'users/:id/edit, to: 'users#update'

My guess is I have a problem in my routes, and there should be no need to call a 'update' method, it should do this in the edit_user_path right? OR do I need to call the method? Sorry just confused here between routes and calling methods and how that works with rails. Thank very much for any help.

Aucun commentaire:

Enregistrer un commentaire