Reputation: 113
On a page a user is prompted to enter their location, twitter screenname, Facebook URL, and LinkedIn URL. What is missing that is not allowing it to be saved in the database?
User model (user.rb):
class User < ActiveRecord::Base
has_many :chatpost, dependent: :destroy
before_save { self.email = email.downcase }
before_create :create_remember_token
attr_accessor :twitter_id, :facebook, :linkedin, :location
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }, :unless => lambda{ |user| user.password.blank? }
# Convert user's name to friendly url format
def slug
name.downcase.gsub(" ", "-")
end
# Change default param for user from id to id-name for friendly urls.
# When finding in DB, Rails auto calls .to_i on param, which tosses
# name and doesn't cause any problems in locating user.
def to_param
"#{id}-#{slug}"
end
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.encrypt(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.encrypt(User.new_remember_token)
end
end
Users controller (users_controller.rb):
class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
skip_before_filter :require_login, :only => [:new, :create]
def index
@user
end
def show
# TODO: Redirect to canonical lins if slugs change
# see: http://code-worrier.com/blog/custom-slugs-in-rails/
@user = User.find(params[:id])
end
def new
#redirect if we are already signed in
if signed_in?
flash[:notice] = "Already signed in"
redirect_to root_path
end
#create a new variable to hold User params
@user = User.new
end
def create
@user = User.new(user_params)
#Make the first user created a super user
if User.count == 0
@user[:super_user] = true
else
@user[:super_user] = false
end
if @user.save
sign_in @user
flash[:success] = "Welcome to LivePost!"
redirect_to chatlogs_path
else
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
params[:user].delete(:password) if params[:user][:password].blank?
if @user.update_attributes(params[:user].permit(:name, :email, :password, :password_confirmation, :twitter_id, :facebook, :linkedin, :location))
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted."
redirect_to root_path
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :twitter_id, :facebook, :linkedin, :location)
end
# Before filters
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
Users helper (users_helper.rb):
module UsersHelper
# Returns the Gravatar for the given user (see: http://gravatar.com/)
def gravatar_for(user)
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
image_tag gravatar_url, alt: user.name, class: "gravatar"
end
def twitter_for(user)
user.twitter_id ? "https://www.twitter.com/#{user.twitter_id}" : ""
end
def facebook_for(user)
#{user.facebook}
end
def linkedin_for(user)
#{user.linkedin}
end
end
User profile edit (views/users/edit.html.erb):
<% provide(:title, 'Edit profile') %>
<div class="showuser">
<row>
<div class="col-md-6 col-md-offset-3" style="margin-top: 6%;">
<div class="panel panel-default" style="padding-bottom: 15px;padding-left: 10px;">
<div class="panel panel-body">
<h3 class="form-signin-heading" align="center"><b><%= @user.name.titleize %>'s profile</b></h3>
<%= form_for @user, class: "form-signin" do |f| %>
<%= form_tag :action => 'update', :id => @user %>
<%= render 'shared/error_messages', object: f.object %>
<div align="left"><span class="label label-default">Email</span></div>
<%= f.text_field :email, class: "form-control", disabled: true, style: "color: #999;" %>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
General User Information
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<div align="left"><span class="label label-default">Name</span></div>
<%= f.text_field :name, class: "form-control", placeholder: "Your name" %>
<div align="left"><span class="label label-default">Location</span></div>
<%= f.text_field :location, class: "form-control", placeholder: "Your location" %>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
Avatar
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
<div>
<%= gravatar_for @user %>
<a href="http://gravatar.com/emails">Change avatar</a>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
Social Media
</a>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse">
<div class="panel-body">
<div align="left"><span class="label label-default">Twitter</span></div>
<%= f.text_field :twitter_id, class: "form-control", placeholder: "Twitter username" %>
<div align="left"><span class="label label-default">Facebook</span></div>
<%= f.text_field :facebook, class: "form-control", placeholder: "Facebook profile URL" %>
<div align="left"><span class="label label-default">LinkedIn</span></div>
<%= f.text_field :linkedin, class: "form-control", placeholder: "LinkedIn profile URL" %>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseFour">
Password
</a>
</h4>
</div>
<div id="collapseFour" class="panel-collapse collapse">
<div class="panel-body">
<%= f.password_field :password, class: "form-control", placeholder: "Password" %>
<%= f.password_field :password_confirmation, class: "form-control", placeholder: "Confirm password" %>
</div>
</div>
</div>
<br>
<div align="center">
<%= link_to "Cancel", @user, style: "margin-right: 50px;" %>
<%= f.submit "Update", class: "btn btn-large btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
</div>
</row>
</div>
I've been somewhat stuck on this either not finding clear answers or not sure what to do (for example use Self). I've tried to solve this one for awhile and feel I'm missing something obvious but can't quite figure out what it is. Any help would be much appreciated, I'm still very much new to Ruby and RoR.
Edit: Rails server:
Started PATCH "/users/1-test" for 209.240.97.186 at 2013-12-24 14:43:38 +0000
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"YemNvIfdYxnChL5tsQxVXNxHLUHIOPeDJ45tHTiyN5s=", "user"=>{"name"=>"Test Addington", "location"=
>"California", "twitter_id"=>"", "facebook"=>"", "linkedin"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Up
date", "id"=>"1-test"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'b6b3e86b6a7d3b77641f32d72d7f68b035944cac' LIMIT 1
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1-test"]]
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('[email protected]') AND "users"."id" != 1) LIMIT 1
SQL (0.4ms) UPDATE "users" SET "name" = ?, "updated_at" = ? WHERE "users"."id" = 1 [["name", "Test Addington"], ["updated_at", Tue, 24 Dec 2
013 14:43:38 UTC +00:00]]
(50.8ms) commit transaction
Redirected to http://kingswood-rails-1-64062.euw1.actionbox.io:3000/users/1-test-addington
Completed 302 Found in 58ms (ActiveRecord: 52.0ms)
Started GET "/users/1-test-addington" for 209.240.97.186 at 2013-12-24 14:43:38 +0000
Processing by UsersController#show as HTML
Parameters: {"id"=>"1-test-addington"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'b6b3e86b6a7d3b77641f32d72d7f68b035944cac' LIMIT 1
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1-test-addington"]]
Rendered users/show.html.erb within layouts/application (0.9ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_sidemenu.html.erb (0.7ms)
Rendered layouts/_flash_messages.html.erb (0.1ms)
Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 25ms (Views: 22.9ms | ActiveRecord: 0.5ms)
Upvotes: 0
Views: 593
Reputation: 15917
Okay, based upon the comments you've made, this may be your problem...
In your update
method, you're not using the user_params
method you've already created for this purpose, and the params you are using seem a bit strange to me even though they don't seem to be generating any errors.
So, my suggestion would be to use the following in your update
method:
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
Upvotes: 0
Reputation: 3083
your params are coming blank thats why value is not getting saved
Parameters: {"utf8"=>"✓", "authenticity_token"=>"YemNvIfdYxnChL5tsQxVXNxHLUHIOPeDJ45tHTiyN5s=", "user"=>{"name"=>"Test Addington", "location"=
>"California", "twitter_id"=>"", "facebook"=>"", "linkedin"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Up
date", "id"=>"1-test"}
Upvotes: 1