0

您将在我的代码中看到我有一个“has_many => belongs_to”模型关联和一个嵌套表单在新操作的视图中。

另外,我在 Rails 表单处理程序中使用了对同一参数使用两个单独的字段?

问题是“prix”属性(在“Projet”模型中)记录在数据库中,而不是“nom”属性(在“Activite”模型中)。

也许问题出在强参数周围,但我认为这在我的代码中都很好……或者在我在链接的另一个 Stackoverflow 问题上找到的代码中。

我的代码中有法语单词:activite 是活动,projet 是项目,nom 是名称,prix 是价格,非常简单:)

谢谢你的帮助 !

应用程序/模型/projet.rb:

class Projet < ActiveRecord::Base

    has_many :activites

    accepts_nested_attributes_for :activites,
    reject_if: lambda {|attributes| attributes['nom'].blank?}

end

应用程序/模型/activite.rb:

class Activite < ActiveRecord::Base

    belongs_to :projet

    def acttext=(value)
      @acttext = value if value
      prepare_act
    end

  def actselect=(value)
    @actselect = value if value
    prepare_act
  end

  def acttext
    @acttext || self.nom
  end

  def actselect
    @actselect || self.nom
  end

private 
  def prepare_act
    self.nom = acttext if acttext
    self.nom = actselect if actselect
  end

end

应用程序/控制器/projets_controller.rb:

class ProjetsController < ApplicationController

    def new
        @projet = Projet.new
        @activites_options = Activite.pluck(:nom).uniq
        2.times { @projet.activites.new}
    end

    def create
        @projet = Projet.new(projet_params)

        if @projet.save
          redirect_to @projet
        else
          render 'new'
        end
    end

  private

    def projet_params
      params.require(:projet).permit(:prix, activites_attributes: [:id, :nom, :heures, :minutes, :acttext, :actselect])
    end
end

app/views/projets/new.html.erb :

<div>
    <%= form_for @projet do |f| %>    
    <%= f.label :prix %><br> 
    <%= f.text_area :prix %><br>
            <ul>
              <%= f.fields_for :activites do |activites_form| %>
                <li>
                  <%= activites_form.label :choisissez_un_type_dactivité %>
                  <%= activites_form.select :actselect, @activites_options, {include_blank: true} %>

                  <%= activites_form.label :ou_créez_en_un_nouveau %>
                  <%= activites_form.text_field :acttext %><br>

                  <%= activites_form.label :heures %>
                  <%= activites_form.text_field :heures %>

                  <%= activites_form.label :minutes %>
                  <%= activites_form.text_field :minutes %>
                </li>
                <br>
              <% end %>
            </ul>
    <p><%= f.submit "Créer le projet" %></p>
    <% end %>
    </div>
4

1 回答 1

0

为了使虚拟属性起作用,您还需要定义一个 setter 和一个 getter。这可以通过自己明确定义 getter 和 setter 或使用attr_accessor.

使用示例attr_accessor

class Activite < ActiveRecord::Base
  attr_accessor :nom
  ....

end

手动定义 setter 和 getter 的示例:

class Activite < ActiveRecord::Base
  ....

  def nom=(value)
    super
  end

  def nom
    @nom
  end
end

对于由于它们是法语而导致的类命名的第二个问题,您还需要class_name在两者上指定选项has_manybelongs_to指定非英语类名称,如下所示:

class Projet < ActiveRecord::Base
  has_many :activites, class_name: 'Activite'
end

同样,对于Activite模型:

class Activite < ActiveRecord::Base
  belongs_to :projet, class_name: 'Projet'
end
于 2015-07-30T13:28:12.177 回答