【问题标题】:Rails 5.1.6 params not savingRails 5.1.6 参数不保存
【发布时间】:2018-06-05 21:07:54
【问题描述】:

自从我使用 Rails 做很多事情以来已经有几年了,而且我还没有使用 Rails 5.1.6 的经验。虽然我能够通过种子文件和 Rails 控制台保存数据,但我无法从 HTML 表单中保存数据。提交时,我没有保存,而是看到身份验证令牌和参数出现在浏览器的 URL 栏中,并重新呈现了表单。

唯一不寻常的是我在 Docker 容器中开发,但我在 5.1.4 中编写的最后一个应用程序使用相同的 Dockerfile 和 Docker-Compose 文件,并且运行正常。

我的控制器:

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

  def index
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to @user
    else
      render :new
    end
  end

  def show
  end

  def edit
  end

  def update
    if @user.update_attributes(user_params)
      redirect_to users_url
    else
      render :edit
    end
  end

  private
    def user_params
      params.require(:user).permit(:last_name, :first_name, :category, :phone_number, :email, :password, :password_confirmation)
    end

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

观点:

<%= form_with model: (@user) do |f| %>
      <%= f.label :first_name %>
      <%= f.text_field :first_name %>

      <%= f.label :last_name %>
      <%= f.text_field :last_name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :phone_number %>
      <%= f.text_field :phone_number %>

      <%= f.submit "Create user" %>
<% end %>

型号:

class User < ApplicationRecord
  belongs_to :account
end

开发日志内容:

Started GET "/users/new?utf8=%E2%9C%93&authenticity_token=r0JIvUKGw1mtIVHT582uIfVvTglMTn6PE2iYOCYNHDv5GJ5AedXb4ZS%2F7kFRmfmFA5PIJeklSbmSE3FR0SpJSg%3D%3D&user%5Bfirst_name%5D=aaa&user%5Blast_name%5D=bbb&user%5Bemail%5D=aaa%40bbb.com&user%5Bphone_number%5D=&commit=Create+user" for 172.18.0.1 at 2018-06-05 21:02:12 +0000
Processing by UsersController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"r0JIvUKGw1mtIVHT582uIfVvTglMTn6PE2iYOCYNHDv5GJ5AedXb4ZS/7kFRmfmFA5PIJeklSbmSE3FR0SpJSg==", "user"=>{"first_name"=>"aaa", "last_name"=>"bbb", "email"=>"aaa@bbb.com", "phone_number"=>""}, "commit"=>"Create user"}
  Rendering users/new.html.erb within layouts/application
  Rendered users/new.html.erb within layouts/application (2.0ms)
Completed 200 OK in 348ms (Views: 331.7ms | ActiveRecord: 0.0ms)

“提交”后的网址:

http://localhost:3000/users/new?utf8=%E2%9C%93&authenticity_token=r0JIvUKGw1mtIVHT582uIfVvTglMTn6PE2iYOCYNHDv5GJ5AedXb4ZS%2F7kFRmfmFA5PIJeklSbmSE3FR0SpJSg%3D%3D&user%5Bfirst_name%5D=aaa&user%5Blast_name%5D=bbb&user%5Bemail%5D=aaa%40bbb.com&user%5Bphone_number%5D=&commit=Create+user

我以前见过这种情况,但可能已经是三年前了,我不记得我是如何解决它的。任何想法将不胜感激。

【问题讨论】:

  • 您在问题中显示的GET 之前是否有POST
  • 不,没有。但是,逻辑告诉我应该有。我对“form_with”的使用在某些方面不正确吗?
  • 纯粹出于好奇,如果删除@user 周围的括号会发生什么(不添加local: true)?我有一种感觉 Salil 正在努力。
  • 我尝试了两种方法都没有成功。我在一个类似的容器中查看了一个 Rails 5.1.4 应用程序,在那个应用程序中,“for_with model:@model_name,local:true”POSTS。在这种情况下,它会提交一个 GET 请求。
  • 我解决了。我的视图是位于 HTML 表单标签(而不是 div)内的部分视图。我将表单标签更改为 div 标签,GET 变成了 POST。我将 Salil 的解决方案标记为正确的解决方案,因为我仍然需要使用“local: true”。还要感谢 jvilian 让我对此进行深入思考。

标签: ruby-on-rails rails-5.1.6


【解决方案1】:

检查this 改变

<%= form_with model: (@user) do |f| %>

<%= form_with model: @user, local: true do |f| %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-24
    • 2018-10-23
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多