Rails表单输入未保存,提交后变为空白

时间:2019-05-27 01:46:09

标签: ruby-on-rails

在这里启动初学者!

我制作了一个用于编辑用户信息的表格,并且尝试编辑用户的全名,该全名最初是空白的。但是,当我按Submit时,全名字段将变为空白,并且表单告诉我,由于全名不能为空白,因此无法保存更改。我见过同样的人,但是他们有这个问题,因为他们有宝石“ protected_attributes”。

这是我的服务器输出:

    Started PATCH "/users/9b62e326-ca9f-47b9-9e91-256e42ca52fc" 
    for 127.0.0.1 at 2019-05-26 21:30:22 -0400
    Processing by UsersController#update as HTML
    Parameters: {"utf8"=>"✓","authenticity_token"=>"G8zS2fWVdOBAc==", "user"=>{"full_name"=>"", 
    "email"=>"email@gmail.com"}, "commit"=>"Update User", 
    "id"=>"9b62e326-ca9f-47b9-9e91-256e42ca52fc"}
    User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE 
    "users"."slug" = ? LIMIT ?  [["slug", "9b62e326-ca9f-47b9- 
    9e91-256e42ca52fc"], ["LIMIT", 1]]
    ↳ app/controllers/users_controller.rb:57 (0.1ms)  begin transaction
    ↳ app/controllers/users_controller.rb:37
   (0.1ms)  rollback transaction
   ↳ app/controllers/users_controller.rb:37
   Rendering users/edit.html.erb within layouts/application
   Rendered users/_form.html.erb (6.6ms)
   Rendered users/edit.html.erb within layouts/application 
   (12.7ms)
   Category Load (0.3ms)  SELECT "categories".* FROM "categories"
   ↳ app/views/layouts/application.html.erb:31
   User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE 
   "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], 
   ["LIMIT", 1]]
   ↳ app/views/layouts/application.html.erb:38
   Completed 200 OK in 137ms (Views: 126.5ms | ActiveRecord: 1.4ms)

这是我的表单代码:

    <%= form_for @user, html: { multipart: true } do |f| %>

    <% if @user.errors.any? %>
    <h2><%= pluralize(@user.errors.count, "error")%> prevented 
    this post from saving:</h2>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
    <% end %>
    </ul>
    <% end %>
    <%= f.label :full_name, "Full Name" %>
    <%= f.text_field :full_name %>

    <%= f.label "Upload profile picture" %><br><br />
    <%= f.file_field :profile %><br/><br/>
    <br>



   <%= f.label "Email (Will not be published)" %><br>
   <%= f.text_field :email %>


   <% @full = @user.full_name %>

   <%= f.hidden_field :full_name, :value => @full %>

用户控制器:

    class UsersController < ApplicationController
    before_action :find_user, only: [:show, :edit, :update, :destroy]
    before_action :authenticate_user!, only: [:show]



      def show
        @user = User.find(params[:id])
        @user_posts = @user.posts.all.order("created_at desc")
        @posts = Post.all.order("created_at desc")
      end

     def index
        @users = User.all.order("created_at desc")
     end

     def edit
     end

     def new
        @user = User.new(:email => 'test@example.com', :password => 
        'password', :password_confirmation => 'password', :full_name 
        => 'Jane Doe')
        @user.save
     end

    def create
        @user = User.new(user_params)
        @user.save

        if @user.save
            redirect_to @user, notice: "Your changes were successfully 
             saved!"
        else
            render 'new', notice: "Oh no! Your changes were not able 
            to be saved!"
        end
        end

        def update
          if @user.update_attributes(user_params)
          redirect_to @user, notice: "Your changes were saved!"
          else
             render 'edit'
          end
        end

        def destroy
           @user.destroy
           redirect_to users_path
        end

    private

    def user_params
       params.require(:user).permit(:email, :password, 
       :password_confirmation, :full_name, :slug)

    end

    def find_user
      @user = User.friendly.find(params[:id])
    end
    end

User.rb

    class User < ApplicationRecord
    extend FriendlyId
    friendly_id :full_name, use: :slugged

     devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :validatable
     has_many :posts


     validates :full_name, presence: true

     validates :email, presence: true

     def should_generate_new_friendly_id?
     slug.blank? || full_name_changed?
     end
     end

我的帖子模型也使用了相同的表单格式,所以我不确定为什么会有问题。 感谢您的任何帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

同一格式的同一属性,您不应具有text_field和hidden_​​field

您可以删除

<% @full = @user.full_name %>
<%= f.hidden_field :full_name, :value => @full %>

您已经拥有的表格

<%= f.label :full_name, "Full Name" %>
 <%= f.text_field :full_name %>

答案 1 :(得分:1)

在表单代码中,有两个全名字段,一个是文本字段,另一个是隐藏字段。事件,如果您输入值,则隐藏值会将其设置为null。因此,如果您删除它,它将起作用。

 
    <% if @user.errors.any? %>
    <h2><%= pluralize(@user.errors.count, "error")%> prevented 
    this post from saving:</h2>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
    <% end %>
    </ul>
    <% end %>
    <%= f.label :full_name, "Full Name" %>
    <%= f.text_field :full_name %>

    <%= f.label "Upload profile picture" %><br><br />
    <%= f.file_field :profile %><br/><br/>
    <br>



   <%= f.label "Email (Will not be published)" %><br>
   <%= f.text_field :email %>