form_for嵌套属性:create不会保存到数据库

时间:2019-11-27 14:43:20

标签: ruby-on-rails

我有一个用户模型和一个个人档案模型。用户

has_one :profile, dependent: :destroy
after_create :create_profile
has_many :reviews, dependent: :destroy
accepts_nested_attributes_for :profile

因此,在sign_up用户被重定向到new_user_profile_path以创建配置文件

class Users::RegistrationsController < Devise::RegistrationsController
    protected
      def after_sign_up_path_for(resource)
        new_user_profile_path(current_user.id)
      end
end

Profile#new具有form_for嵌套属性,其外观如下

<%= form_for(@user, url: user_profile_path(@user), method: :post) do |f| %>
  <%= f.fields_for :profile, @profile do |profile_fields| %>
    <div class="field">
      <%= profile_fields.label :about %>
      <%= profile_fields.text_area :about %>
    </div>

    <div class="field">
      <%= profile_fields.file_field :avatar %>
      <% profile_fields.label "Profile photo" %>
    </div>

  <% end %>


    <div class="field">
        <%= f.label :street %>
        <%= f.text_area :street %>
    </div>

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

<% end %>

问题在于,此表单既不会保存到用户,也不会保存到配置文件。参数传递如下

Parameters: {"utf8"=>"✓", "authenticity_token"=>"some_token", "user"=>{"profile_attributes"=>{"about"=>"mpl", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007fb710cc4a88 @tempfile=#<Tempfile:/var/folders/0q/y8_xn1q57wn1_x9zhph1fpz00000gn/T/RackMultipart20191127-9283-sjkiqb.jpg>, @original_filename="055g.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[profile_attributes][avatar]\"; filename=\"055g.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "street"=>"nmp"}, "commit"=>"Update User", "user_id"=>"14"}

该表单具有一个提交按钮,默认情况下,该按钮显示为“更新用户”,因此我想也许我需要在用户中使用一种更新方法才能使其正常工作。但这没有帮助。这是我的UsersController

class UsersController < ApplicationController

  def index
    @users = User.all
  end

  def new
    @user = User.new
  end

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

  def update
    current_user.update_attributes(user_params)
    redirect_to user_profile_path(@user)
  end

  private

  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :street, :house_number, :city, :zip_code, profile_attributes: [:about, :avatar])
  end

end

还有我的ProfilesController:

class ProfilesController < ApplicationController
  before_action :set_profile, only: [:show, :edit, :update, :destroy]

  def show
    @user = User.eager_load(:profile).find(params[:user_id])
    @profile = @user.profile
    @review = Review.new
    @reviews = Review.where(profile: @profile) 
  end

  def new
    @user = current_user
    @profile = Profile.new
  end

  def edit
    @profile = @user.profile
  end

  def create
    @user = current_user
    @profile = @user.build_profile(profile_params)

    respond_to do |format|
      if @profile.save
        format.html { redirect_to user_profile_path(current_user.id), notice: 'Profile was successfully created.' }
        format.json { render :show, status: :created, location: @profile }
      else
        format.html { render :new, notice: 'Did not save' }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @profile.update(profile_params)
        format.html { redirect_to user_profile_path(current_user.id), notice: 'Profile was successfully updated.' }
        format.json { render :show, status: :ok, location: @profile }
      else
        format.html { render :edit }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @profile.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'Profile was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    def set_profile
      @profile = current_user.profile
    end

    def profile_params
      params.permit(:about, :avatar)
    end

end

我在每个地方都看过,也在这里以Unpermitted Parameters Accepts Nested Attributes的形式出现,但是我没有做任何事情将其保存到DB。而且,我尝试过

params.require(:profile).permit(:about, :avatar)

但是它返回了一个非常奇怪的错误:

ActiveSupport::MessageVerifier::InvalidSignature @profile = Profile.new(profile_params)

我在做什么错?我是Rails的新手,我真的很困。

P.S。这是我的UserModel

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :omniauthable, omniauth_providers: %i[facebook]


    has_one :profile, dependent: :destroy
    after_create :create_profile
    has_many :reviews, dependent: :destroy
    accepts_nested_attributes_for :profile
    validates :first_name, presence: true
    validates :last_name, presence: true

    def self.from_omniauth(auth)
        where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
            user.email = auth.info.email
            user.password = Devise.friendly_token[0, 20]
            name = auth.info.name
            user.first_name = name.split(" ")[0]
            user.last_name = name.split(" ")[1] 
        end
    end
end

1 个答案:

答案 0 :(得分:1)

因此,这似乎是对我的问题的一个很好的答案,但不是解决方案,所以也许有人想对此发表评论。对我来说,这是一个非常新颖有趣的东西。主要的挑战似乎是验证部分对象。在sign_up时,我只提交名称,电子邮件和密码,然后在下一步骤中请求strett,我将单个模型分为两种形式。跨表格的模型拆分意味着您不得不问一些尴尬的问题,例如:“嘿,ActiveRecord,到目前为止,我拥有的这个对象的一半有效吗?”我认为ActiveRecord不是设计用于验证对象的一部分的。

显然,有一个名为Wicked的宝石,它一步一步地处理“构建部分对象”-看起来对我来说太专业了。但是,如果有人愿意支持我尝试一下,那就太好了。关于此主题的文章也不错,我从https://www.honeybadger.io/blog/multi-step-forms-in-rails/