使用Rails4中的update_attributes更新表单数据

时间:2015-09-06 11:38:12

标签: ruby-on-rails forms postgresql ruby-on-rails-4 postgresql-9.3

我正在尝试使用"更新"更新用户表中的用户数据。用户"中的行动Rails4中的控制器。我正在使用PostgreSQL数据库。

问题:当我去"编辑"动作,我得到正确呈现的表单。在表格中的1列或多列填写数据后,当我按下"更新档案"按钮更新用户的数据,然后形成页面重定向并返回到相同的编辑页面,但现在网址已更改。数据未保存在数据库中。它仅显示在表单字段中。甚至个人资料图片也在上传,但他们的链接没有保存在数据库中。

示例:

更新前 - >网址:http://localhost:3000/users/edit?id=2

更新后 - >网址:http://localhost:3000/users/update/2

但新输入的数据并未保存在数据库中。我还检查了rails服务器日志,但我没有理解,表单有什么问题。通过rails控制台我试图更新用户的数据,但我仍然无法在数据库中保存数据。请查看截图。

Rails服务器日志: enter image description here

Rails控制台: enter image description here

用户控制器:

    class UsersController < ApplicationController
      def edit
        @user = User.find("#{@current_user_id}")
      end

      def update
        User.transaction do
          @user = User.find(params[:id]).lock!
           if @user.update_attributes(user_params)
             Welcome.profile_data_updated(@user).deliver_now
             redirect_to(:controller => 'users', :action => 'profile', :id => @current_user_id)
           else
             render('edit')
           end
        end
      end
    protected
  def user_params
    params.require(:user).permit(:username, :user_image, :first_name, :last_name, :password, :email, :street_address, :city, :state, :country, :postal_code, :mobile_number)
  end
end

CreateUsers迁移文件:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
    t.string :username, null: false
    t.string :user_image
    t.string :first_name, null: false
    t.string :last_name, null: false
    t.string :password_digest, null: false
    t.string :email, null: false
    t.string :street_address
    t.string :city
    t.string :state
    t.string :country
    t.string :postal_code
    t.string :mobile_number
    t.string :validation_code
    t.string :user_status, null: false, :default => '1' # 0 for deactivated
    t.timestamps null: false
    end
  end

  def down
    drop_table :users
  end
end

用户模型:

class User < ActiveRecord::Base
    has_secure_password
    validates_confirmation_of :password

    include CarrierWave::RMagick    
    mount_uploader :user_image, ImageUploader


    validates_presence_of :username, :format => {:with => /\A[a-zA-Z0-9]+\Z/, :message => 'Username can only contain Alphabets'}
    validates_presence_of :first_name,:last_name,:password,:email
    validates_uniqueness_of :username, :email
    validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :on => :create
    validates_numericality_of :postal_code, :mobile_number, :on => :update

    def full_name
        "#{first_name} #{last_name}"
    end 

    def location
        "#{street_address}, #{city}, #{state}, #{country}, #{postal_code}"
    end
end

Update.html.erb文件:

<%= form_for @user, url: { action: "update", :id => @user.id }, method: :post, html: { multipart: true, class: "ui form"} do |f| %>
        <div align="center"><%= image_tag @user.user_image.to_s, :size => "200x200" %></div>
        </br>
        <div align="center">
        <%= f.file_field :user_image %>
        </div>

    <h4 class="ui dividing header">Billing Information</h4>
    <div class="field">
        <%= f.label "Name" %>

        <div class="three fields">
            <div class="field">
                <%= f.text_field :username, :placeholder => 'Username' %>
            </div>
            <div class="field">
                <%= f.text_field :first_name, :placeholder => 'First Name' %>
            </div>
            <div class="field">
                <%= f.text_field :last_name, :placeholder => 'Last Name' %>
            </div>
        </div>
    </div>

    <div class="field">
        <%= f.label "Billing Address" %>
            <div class="fields">
                <div class="ten wide field">
                    <%= f.text_field :street_address, :placeholder => 'Street Address' %>
                </div>
                <div class="three wide field">
                    <%= f.text_field :city, :placeholder => 'City' %>
                </div>
                <div class="three wide field">
                    <%= f.text_field :postal_code, :placeholder => 'Postal Code' %>
                </div>
            </div>
    </div>

    <div class="two fields">
        <div class="field">
            <%= f.label "State" %>
            <%= f.text_field :state, :placeholder => 'State' %>
        </div>
        <div class="field">
            <%= f.label "Country" %>
            <%= f.text_field :country, :placeholder => 'Country' %>
        </div>
    </div>

    <h4 class="ui dividing header">Contact Information</h4>
    <div class="two fields">
        <div class="disabled field">
            <%= f.label "Email" %>
            <%= f.text_field :email, :disabled => true %>
        </div>
        <div class="field">
            <%= f.label "Contact" %>
            <%= f.text_field :mobile_number, :placeholder => 'Mobile Number' %>
        </div>
    </div>

    <h4 class="ui dividing header">Security</h4>
    <div class="two fields">
        <div class="field">
            <%= f.label "New Password" %>
            <%= f.password_field :password, :placeholder => 'New Password' %>
        </div>
        <div class="field">
            <%= f.label "Retype Password" %>
            <%= f.password_field :password_confirmation, :placeholder => 'Retype Password' %>
        </div>
    </div>
      <div>
        <%= f.submit "Update Profile", data: { disable_with: "Updating..." }, class: 'ui green button pull-right'%>
      </div>
<% end %>

我的意见:

  1. 可能在&#34;更新&#34;中有错误行动。但是如果有任何错误,那么Rails必须在处理表单时抛出错误。
  2. 受保护的方法&#34; user_params&#34;也许保护数据不被提交。因为我没有填写所有表格栏。我在1到多个表单字段中输入数据。
  3. 编辑表格可能存在错误。
  4. 新服务器日志: enter image description here

    具有注册操作的用户控制器:

    def signup
        @user = User.new
      end
    
      def create
        @user = User.new(user_params)
        if @user.save
            Welcome.welcome(@user).deliver_now
            redirect_to(:controller => 'access', :action => 'login')
        else
          render('signup')
        end
      end
    

    注册表格:

    <%= form_for(:user, :html => {:multipart => true }, :url => {:controller => 'users',:action => 'create'})  do |f| %>
    
                        <h3 class="nomargin">Sign Up</h3>
                        <p class="mt5 mb20">Already a member? <%= link_to "Login" , {:controller => 'access', :action => 'login'} %><br><br>
    
                        <label class="control-label">Username</label>
                        <%= f.text_field :username, class: 'form-control' ,placeholder:'Username' %></br>
    
                        <label class="control-label">Email</label>
                        <%= f.text_field :email, class: 'form-control' ,placeholder:'Email' %></br>
    
                        <label class="control-label">Profile Picture</label>
                        <%= f.file_field(:user_image) %></br>
    
                        <label class="control-label">Name</label>
                        <div class="row mb10">
                            <div class="col-sm-6">
                                <%= f.text_field :first_name, class: 'form-control', placeholder: 'First Name' %></br>
                            </div>
                            <div class="col-sm-6">
                                <%= f.text_field :last_name, class: 'form-control', placeholder: 'Last Name' %></br>
                            </div>
                        </div>
    
                        <div class="mb10">
                            <label class="control-label">Password</label>
                            <%= f.password_field :password, class: 'form-control', placeholder: 'Password' %></br>
                        </div>
    
                        <div class="mb10">
                            <label class="control-label">Retype Password</label>
                             <%= f.password_field :password_confirmation, class: 'form-control', placeholder: 'Retype Password' %></br>
                        </div>
    
                        <button class="btn btn-success btn-block">Sign Up</button>     
                    <% end %>
    

1 个答案:

答案 0 :(得分:0)

发布的第一个日志中的错误表示更新后的未允许参数=&gt; :password_confirmation

请尝试在您的强参数验证中添加:password_confirmation。

def user_params
    params.require(:user).permit(:username, :user_image, :first_name, :last_name, :password, :email, :street_address, :city, :state, :country, :postal_code, :mobile_number, :password_confirmation)
  end

请告诉我它是怎么回事。

强参数更改后的更新部分包括表单字段password_confirmation和讨论

请尝试更改此内容:

    class UsersController < ApplicationController
      def edit
        @user = User.find("#{@current_user_id}")
      end

对此:

      class UsersController < ApplicationController 
      # Precursor action to rendering the edit user view
        def edit
          @user = User.find(params[:id])
        end