【问题标题】:Form does not submit data表单不提交数据
【发布时间】:2017-03-20 05:23:37
【问题描述】:

我有一个表单,其中有一些字段应该填充到我的数据库中,但它没有。我不确定这是为什么。我定义了修复另一个错误的参数,但我的数据没有被存储。我的表名是 users。

我的控制器:

 Class LandlordPageController < ApplicationController
      before_action :get_user

      def get_user
        @user = current_User
      end

      def index
      end

      def show
      end

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

      private

        def user_params
          params.require(:user).permit(:address, :cityResiding, :ssn, :birthDate, :gender, :phoneNumber)
      end
end

我的表格:

<%= form_for :user do |f| %>

    <div class="field">
      <%=f.label :address, 'Current Address' %><br/>
      <%=   f.text_field :address, :required => 'required' %>
    </div>

    <div class="field">
      <%=f.label :cityResiding, 'Current City' %><br/>
      <%=   f.text_field :cityResiding, :required => 'required' %>
    </div>

    <div class="field">
      <%=f.label :phoneNumber, 'Phone Number'%><br/>
      <%= f.telephone_field :phoneNumber, maxlength: 15, :required => 'required' %>
    </div>

    <div class="field">
      <%=f.label :gender, 'Gender'%><br/>
      <%= f.select :gender, ['',' Male', 'Female', 'Other','Prefer Not to Answer']%>
    </div>

    <div class="field">
      <%=f.label :birthDate, 'Birth Date'%><br/>
      <%= f.date_select :birthDate, order: [:month, :day, :year], :required => 'required'%>
    </div>

    <div class="field">
      <%=f.label :ssn, 'Social Security Number' %><br/>
      <%=   f.text_field :ssn, maxlength: 9 %>
    </div>

    <div class="actions">
      <%= f.submit "Submit Information" %>
    </div>

<% end %>

日志:

Started GET "/landlord_page" for 127.0.0.1 at 2016-11-06 17:59:58 -0500
Processing by LandlordPageController#index as HTML
  User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Rendering landlord_page/index.html.erb within layouts/application
  Rendered landlord_page/index.html.erb within layouts/application (4.0ms)
  Rendered layouts/_navbar.html.erb (1.0ms)
Completed 200 OK in 98ms (Views: 88.4ms | ActiveRecord: 0.0ms)


Started POST "/landlord_page" for 127.0.0.1 at 2016-11-06 18:00:06 -0500
Processing by LandlordPageController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"98tUidqprFyTG8ZC/tV9TRDIVlV0I+ocnQfKTUDqorlS+JMFHtaCWz69EwBvH5MrHhnRbg93m695//Z1I5xt3A==", "user"=>{"address"=>"1", "cityResiding"=>"1", "phoneNumber"=>"1", "gender"=>" Male", "birthDate(2i)"=>"11", "birthDate(3i)"=>"6", "birthDate(1i)"=>"2016", "ssn"=>""}, "commit"=>"Submit Information"}
  User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.0ms)  begin transaction
   (0.0ms)  rollback transaction
Redirected to http://localhost:3000/profile_page
Completed 302 Found in 4ms (ActiveRecord: 0.0ms)

编辑:我的表单确实提交了数据,但是保存时抛出了这个错误

NoMethodError in LandlordPageController#create
undefined method `save' for #<Array:0x0000000d883a08>

【问题讨论】:

  • 日志中显示了什么?表格在哪里?
  • 您的模型是否需要任何其他字段?提供您的表格结构也会有所帮助。乍一看,我看不出任何错误。
  • 您可能会遇到验证错误,但无论@user.save 是否成功,您似乎都通过执行redirect_to profile_page_index_path 忽略了这些错误。作为快速测试,在@user.save 之后尝试puts @user.errors 以检查验证错误。
  • 是的,我会处理验证错误,但为了更好地诊断,请将您的 create 方法更改为 @user.save!,这将导致错误被抛出而不是静默消耗。
  • @MarsAtomic 我不知道这是一件事,无论如何这肯定会有所帮助。

标签: ruby-on-rails database forms


【解决方案1】:

而不是new,我使用update 并删除了save,因为新的已经执行保存参数并且save 试图保存nil 参数。我不是在创建 new 用户,而是向现有用户添加新参数。这就是发生错误的原因。完整的最终代码如下。

  def create
    respond_to do |format|
      if User.update(user_params)
        format.html { redirect_to profile_page_index_path, notice: 'Landlord application successfully submitted.' }
        format.json { render :show, status: :created, location: @user }
      else       
        format.html { redirect_to profile_page_index_path, notice: 'Landlord application was not successfully submitted.' }
        format.json { render :show, status: :unprocessable_entity }
      end
    end
  end

【讨论】:

  • create 应该创建 User 而不是更新它。这就是 update 方法/路由的用途
  • @CarlMarkham 那是我出错的地方。我的意思是用这些参数更新现有用户。
  • 您的代码仍然无法运行。你应该在模型@user.update的实例上触发update方法@
  • 请看我的回答。它向您展示了如何以正确的方式做到这一点。此外,如果出现错误,您的代码不会让用户知道出了什么问题。对他们了解错误是否由他们引起是有用的。如果是 xhr 请求,您也不会显示错误
【解决方案2】:

如前所述,问题可能是验证错误。 如果您要更新用户,则需要改用update 方法并将用户传递给表单

form_for @user

一旦提交表单,它将向控制器上的update 方法发送PATCH 请求。

def update
  @user.update user_params
  if ! @user.valid?
    flash[:error] = @user.errors.full_messages
  end

  redirect_to profile_page_index_path
end

【讨论】:

    猜你喜欢
    • 2018-02-03
    • 2015-11-16
    • 2016-07-11
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    相关资源
    最近更新 更多