Ruby on Rails - 编辑/更新创建新记录而不是更新

时间:2016-06-18 20:27:14

标签: ruby-on-rails-4

我已经看到了这个类似问题的其他链接,但它们似乎都不适合我的情况。

我的更新操作是在mysql数据库中创建新记录

我的应用包含3个型号

  1. FashionModel
  2. ModelProfile
  3. Measurement
  4. 它们的定义如下:

    class FashionModel < ActiveRecord::Base
      has_secure_password
      has_one :model_profile
      has_one :measurement
      accepts_nested_attributes_for :model_profile
      accepts_nested_attributes_for :measurement
    end
    
    
    class ModelProfile < ActiveRecord::Base
      belongs_to :fashion_model
    end
    
    
    class Measurement < ActiveRecord::Base
      belongs_to :fashion_model
    end
    

    fashion_model_controller.rb如下:

    class FashionModelsController < ApplicationController
      def new
        @fashion_model = FashionModel.new
      end
    
      def create
        @fashion_model = FashionModel.new(fashion_models_sign_up_params)
        if @fashion_model.save
          flash[:success] = "Welcome to Meriad"
          session[:fashion_model_id] = @fashion_model.id
          redirect_to edit_fashion_model_path(@fashion_model)
        else
          render 'new'
        end
      end
    
      def edit
        @fashion_model = FashionModel.find(params[:id])
        @fashion_model.build_model_profile
        @fashion_model.build_measurement
      end
    
      def update
        @fashion_model = FashionModel.find(params[:id])
        if @fashion_model.update(fashion_models_edit_params)
          flash[:success] = "Saved!"
          redirect_to fashion_model_path(@fashion_model)
        else
          render 'edit'
        end
      end
    
      def show
        @fashion_model = FashionModel.find(params[:id])
      end
    
    end
    

    fashion_model_edit_params

    def fashion_models_edit_params
        params.require(:fashion_model).permit(:first_name,
                                              :last_name,
                                              :email,
                                              :password,
                                              model_profile_attributes: [:id,
                                                                         :location,
                                                                         :bio, :gender,
                                                                         :phone_number,
                                                                         :rate,
                                                                         :profile_image,
                                                                         :birthdate],
                                              measurement_attributes: [:id,
                                                                       :feet,
                                                                       :inches,
                                                                       :bust,
                                                                       :waist,
                                                                       :hips,
                                                                       :dress,
                                                                       :shoes,
                                                                       :hair,
                                                                       :eyes])
      end
    

    我想要这些内容:

    • 时装模特通过new.html.erb(存储在fashion_models表格中)注册到应用程序

    • index.html.erb包含所有fashion_models的列表,其中包含编辑信息的选项(更新其个人资料)

    • edit.html.erb包含model_profiles表中的字段以及measurements表,两者都具有fashion_models表的外键。< / p>

    我的fashion_models/new.html.erb模板非常简单,包含first_namelast_nameemailpassword

    我的fashion_models/edit.html.erb模板类似于:

    <%= form_for @fashion_model do |f| %>
      # fashion_model fields here
    
      <%= f.fields_for :model_profile do |t| %>
        # model_profile fields here
      <% end %>
    
      <%= f.fields_for :measurement do |t| %>
        # measurement fields here
      <% end %>
    
    <% end %>
    

    现在,每当我编辑fashion_model/:id时,model_profilemeasurement都会在数据库中创建新记录,而不是更新现有记录。此外,当我在Edit Profile页面上时,没有任何字段预先填充现有数据。我必须再次手动输入所有数据。

    首先,我认为这是因为build方法,但当我删除它们时,fields_for不会显示。

    感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

你可能会弄乱他们的路线。

它的工作原理如下:

  • form_for来电@fashion_model.new_record?

  • IF True:它会提交给POST /fashion_model,并创建一个对象。

  • IF False:它将提交至PUT /fashion_model/:id,并更新对象。

如您所见,它只取决于对象是否已存在于数据库中。因此,请再次检查您使用form_for的位置以及传递的对象。

有关路线的问题,请参阅:http://guides.rubyonrails.org/routing.html

答案 1 :(得分:0)

所以我终于想出办法来做到这一点。希望它可以帮到某人。

我的问题是我在一个fields_for内有2个form_for,我需要更新这些表格的2个模型。

我厌倦了使用after_action :create_model_profileafter_action :create_measurement等回调。在这两种方法中,我都尝试将fashion_model_id保存为新创建的:id的{​​{1}}。

但由于回调被认为是代码味道,我想要一个更好的解决方案。

我没有创建fashion_model,而是编写了一个如下的辅助方法

form_for @fashion_models

在我的# app/helpers/form_helper.rb module FormHelper def setup_fashion_model(fashion_model) fashion_model.model_profile ||= ModelProfile.new fashion_model.measurement ||= Measurement.new fashion_model end end 中,我使用了类似

的内容
form_for

如果它没有在数据库中保留,则会创建form_for(setup_fashion_model(@fashion_model)) do |f| ... end ModelProfile的新实例。

它当然解决了预先填充包含数据的字段的问题。

就是这样。简单的解决方案,但我一直在那里挠挠脑袋。 希望能帮助到你。

相关问题