用户配置文件输入不会保存到数据库

时间:2013-12-24 14:00:02

标签: ruby-on-rails ruby-on-rails-4

在页面上,系统会提示用户输入其位置,Twitter屏幕名称,Facebook URL和LinkedIn URL。缺少哪些不允许将其保存在数据库中?

用户模型(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.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.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

用户个人资料编辑(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>

我有点坚持这个,要么找不到明确的答案,要么不确定该做什么(例如使用Self)。我试图解决这个问题已经有一段时间了,觉得我错过了一些明显但却无法弄清楚它是什么的东西。任何帮助都会非常感激,我对Ruby和RoR来说还是一个新手。

编辑:Rails服务器:

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@test.com') 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)                                                                                  

2 个答案:

答案 0 :(得分:1)

你的参数变得空白,这就是为什么价值没有得到保存

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"}

答案 1 :(得分:0)

好的,根据您所做的评论,这可能是您的问题......

在您的update方法中,您没有使用为此目的而创建的user_params方法,并且您使用的参数似乎有点奇怪对我而言,即使它们似乎没有产生任何错误。

所以,我的建议是在update方法中使用以下内容:

if @user.update_attributes(user_params)
  flash[:success] = "Profile updated"
  redirect_to @user
else
  render 'edit'
end