Reputation: 197
I'm working on two models with a one to one relationship; the update method is deleting the record it's suppose to update and creating a new one.
The two models - user.rb and profile.rb
USER.RB
class User < ActiveRecord::Base
attr_accessor :remember_token
before_save {self.email = email.downcase }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format:{with: VALID_EMAIL_REGEX},
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
has_one :profile, dependent: :destroy
accepts_nested_attributes_for :profile
end
PROFILE.RB
class Profile < ActiveRecord::Base
validates :name, presence: true, length: { maximum: 50 }
validates :street, :city, :state, :zipcode, presence: true
belongs_to :user
end
Their controllers
USER CONTROLLER
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def new
@user = User.new
@profile = @user.build_profile
end
def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:success] = "Welcome to the Mini Olympics"
redirect_to user_profile_path(current_user, @profile)
else
render 'new'
end
end
def show
@user = User.find(params[:id])
end
def edit
# Commented out the code, as its redundant due to the line 'before_action :correct_user'
# @user = User.find(params[:id])
end
def update
# Commented out first line of the code, as its redundant due to the line 'before_action :correct_user'
# @user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "profile updated"
#redirect_to @user
redirect_to user_profile_path(current_user, @profile)
else
render 'edit'
end
end
def index
@users = User.paginate(page: params[:page], per_page: 15)
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end
private
def user_params
params.require(:user).permit(:id, :email, :password, :password_confirmation, profile_attributes: [:name,
:street, :city, :state, :zipcode] )
end
# Before filters
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user) # '@user == current_user' = 'current_user?(@user)'
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
PROFILE CONTROLLER
class ProfilesController < ApplicationController
def edit
@profile = User.find(params[:user_id]).profile
end
def show
@profile = User.find(params[:user_id]).profile
end
def update
@profile = User.find(params[:user_id]).profile
if @profile.update_attributes(profile_params)
flash[:success] = "profile updated"
redirect_to user_profile_path(current_user, @profile)
else
render 'edit'
end
end
private
def profile_params
params.require(:profile).permit(:id, :name, :street, :city, :state, :zipcode)
end
end
Profile edit form
<% provide(:title, "Edit Profile") %>
<h1>Update your profile</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for [:user, @profile] do |f| %>
<%= render 'fields', f: f %>
<%= f.submit "Save changes", class: "btn btn-primary" %>
<% end %>
</div>
</div>
APP/VIEWS/PROFILES/_FIELDS.HTML.ERB
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :street %>
<%= f.text_field :street, class: 'form-control' %>
<%= f.label :city %>
<%= f.text_field :city, class: 'form-control' %>
<%= f.label :state %>
<%= f.text_field :state, class: 'form-control' %>
<%= f.label :zipcode %>
<%= f.text_field :zipcode, class: 'form-control' %>
Server log
Started GET "/users/1/profile/edit" for 127.0.0.1 at 2015-08-11 20:27:27 -0400
Processing by ProfilesController#edit as HTML
Parameters: {"user_id"=>"1"}
User Load (2.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Profile Load (1.0ms) SELECT `profiles`.* FROM `profiles` WHERE profiles`.`user_id` = 1 LIMIT 1
Rendered profiles/_fields.html.erb (9.0ms)
Rendered profiles/edit.html.erb within layouts/application (30.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
User Load (2.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Rendered layouts/_header.html.erb (15.0ms)
Rendered layouts/_footer.html.erb (2.0ms)
Completed 200 OK in 1896ms (Views: 1874.9ms | ActiveRecord: 5.0ms)
Started PATCH "/users/104/profile" for 127.0.0.1 at 2015-08-11 20:27:34 -0400
Processing by ProfilesController#update as HTML
Parameters: {"utf8"=>"√","authenticity_token"=>"h7gQEkz8JV/u4zT2qX7ivxRz9FLRUi4K42h55mokP/0=", "profile"=>{"name"=>"Example Test", "street"=>"75 Barracks Rd", "city"=>"Water", "state"=>"AW", "zipcode"=>"23455"}, "commit"=>"Save changes","user_id"=>"104"}
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 104 LIMIT 1
Completed 404 Not Found in 47ms
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=104):
app/controllers/profiles_controller.rb:19:in `update'
How did 1 become 104; /users/1/profile/edit to /users/104/profile?
when i update user i.e /users/1/edit, i get
Started GET "/users/1/edit" for 127.0.0.1 at 2015-08-11 19:16:54 -0400
Processing by UsersController#edit as HTML
Parameters: {"id"=>"1"}
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
User Load (2.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Rendered shared/_error_messages.html.erb (57.4ms)
Profile Load (0.0ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
Rendered users/_fields.html.erb (835.0ms)
Rendered users/edit.html.erb within layouts/application (1193.9ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (90.4ms)
Rendered layouts/_footer.html.erb (265.6ms)
Completed 200 OK in 3979ms (Views: 3962.7ms | ActiveRecord: 3.0ms)
Started PATCH "/users/1" for 127.0.0.1 at 2015-08-11 19:17:11 -0400
Processing by UsersController#update as HTML
Parameters:{"utf8"=>"√","authenticity_token"=>"h7gQEkz8JV/u4zT2qX7ivxRz9FLRUi4K42h55mokP/0=", "user"=>{"profile_attributes"=>{"name"=>"Example Test", "id"=>"103", "street"=>"75 Barracks Rd", "city"=>"Water", "state"=>"AW", "zipcode"=>"23455"}, "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Save changes", "id"=>"1"}
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Unpermitted parameters: id (14.0ms) BEGIN
Profile Load (2.0ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
SQL (212.4ms) DELETE FROM `profiles` WHERE `profiles`.`id` = 103
User Exists (143.1ms) SELECT 1 AS one FROM `users` WHERE (`users`.`email` = '[email protected]' AND `users`.`id` != 1) LIMIT 1
SQL (53.0ms) UPDATE `users` SET `password_digest` =$2a$10$1KoyfbAwjKYVlwCsbB4WpObW5D.giibIU3pK5hRYW4.iwc11ZEAVS', `updated_at` = '2015-08-11 23:17:14' WHERE `users`.`id` = 1
SQL (31.0ms) INSERT INTO `profiles` (`city`, `created_at`, `name`, state`, `street`, `updated_at`, `user_id`, `zipcode`) VALUES ('Water', '2015-08-11 23:17:15', 'Example Test', 'AW', '75 Barracks Rd', '2015-08-11 23:17:15', 1, '23455') (94.1ms) COMMIT
Redirected to http://localhost:3000/users/1/profile
Completed 302 Found in 4234ms (ActiveRecord: 551.7ms)
Started GET "/users/1/profile" for 127.0.0.1 at 2015-08-11 19:17:15 -0400
Processing by ProfilesController#show as HTML
Parameters: {"user_id"=>"1"}
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Profile Load (1.0ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
User Load (2.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Rendered profiles/show.html.erb within layouts/application (9.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Rendered layouts/_header.html.erb (9.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 1917ms (Views: 1898.3ms | ActiveRecord: 5.0ms)
I am at a loss at whats triggering the Delete
command, any help is appreciated..
ROUTES FOLDER
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
resource :profile, only: [:show, :edit, :update ]
end
end
Upvotes: 1
Views: 1787
Reputation:
You missed the id
on your list of permitted attributes on UsersController
:
def user_params
params.require(:user).permit(:id, :email, :password, :password_confirmation, profile_attributes: [:id, :name,
:street, :city, :state, :zipcode] )
end
Upvotes: 1