我试图通过在Rails4的“user s”控制器中使用“update”操作来更新users表中的用户数据。我正在使用PostgreSQL数据库。
问题:当我转到“编辑”操作时,我会正确地呈现表单。在表单中的1列或多列中填充数据后,当我按“更新配置文件”按钮更新用户的数据时,表单页将重定向并返回到同一编辑页,但现在url已更改。数据没有保存在数据库中。它只在表单字段中显示。即使是个人资料图片也在上传,但它们的链接没有保存在数据库中。
例子:
更新前->网址:http://localhost:3000/users/edit?id=2
更新后->网址:http://localhost:3000/users/update/2
但新输入的数据没有保存在数据库中。我也检查了rails服务器日志,但是我不明白表单有什么问题。通过rails控制台,我试图更新用户的数据,但仍然无法将数据保存到数据库中。请检查屏幕截图。
Rails服务器日志:
ruby-on-rails - 不使用Rails4中的update_attributes更新表单数据-LMLPHP
轨道控制台:
ruby-on-rails - 不使用Rails4中的update_attributes更新表单数据-LMLPHP
用户控制器:

    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

创建用户迁移文件:
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 %>

我的意见:
也许“更新”操作有错误。但是如果有任何错误,那么Rails在处理表单时一定抛出了一个错误。
受保护的方法“user_params”可能会保护数据不被提交。因为我没有填写所有的表格栏。我正在1到多个表单域中输入数据。
编辑形式可能有错误。
新服务器日志:
ruby-on-rails - 不使用Rails4中的update_attributes更新表单数据-LMLPHP
具有注册操作的用户控制器:
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 %>

最佳答案

发布的第一个日志中的错误表示更新时有一个未经许可的参数=>:password\u确认。
请尝试将:密码确认添加到强参数验证中。

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

请告诉我进展如何。
强参数更改后更新的部分包括表单域密码确认和讨论
请尝试更改此:
    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

10-05 20:33
查看更多