使用嵌套属性保存数据时出现问题

发布于 2024-11-25 08:44:40 字数 3744 浏览 0 评论 0原文

正在 Ruby On Rails 3.0.7 上构建一个网站,我想保存一个 store 对象及其语言。所以,我有以下模型:

class Store < ActiveRecord::Base
  belongs_to :user
  has_many :languages, :through => :store_languages
  has_many :store_languages

  accepts_nested_attributes_for :store_languages

  #Validations
  validates :title, :presence => true, :length => 5..100
  validates :contact_email, :presence => true, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
end

class Language < ActiveRecord::Base
  has_many :stores, :through => :store_languages
  has_many :store_languages
end

class StoreLanguage < ActiveRecord::Base
  belongs_to :store
  belongs_to :language

  validates :store_id, :presence => true
  validates :language_id, :presence => true
end

StoresController 的相关操作:

  def new
    @store = Store.new
    @store.store_languages.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @store }
    end
  end

  # POST /stores
  # POST /stores.xml
  def create
    #raise params.inspect
    @store = current_user.stores.new(params[:store])

    respond_to do |format|
      if @store.save
        format.html { redirect_to(@store, :notice => 'Store was successfully created.') }
        format.xml  { render :xml => @store, :status => :created, :location => @store }
      else
        @store.store_languages.build
        format.html { render :action => "new" }
        format.xml  { render :xml => @store.errors, :status => :unprocessable_entity }
      end
    end
  end

查看:/stores/new.html.erb:

<%= form_for(@store) do |f| %>
  <% if @store.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@store.errors.count, "error") %> prohibited this store from being saved:</h2>

      <ul>
      <% @store.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <label for="title">Title*</label><br />
    <%= f.text_field :title %>
  </p>

  <p>
    <label for="description">Description</label><br />
    <%= f.text_field :description %>
  </p>

  <p>
    <label for="contact_email">Contact E-mail*</label><br />
    <%= f.text_field :contact_email %>
  </p>

  <p>
    <label for="logo">Logo</label><br />
    <%= f.file_field :logo %>
  </p>

    <% f.fields_for :store_languages do |lf| %>

      <%= lf.collection_select :language_id, @languages, :id, :name, {}, {:multiple => true } %>
    <% end %>

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

所以,我在 linguals 表中有以下记录:

 id |   name
 3    English
 4    Español

发生的情况是,当我创建一个新商店,从列表中选择两种语言,它将在 store_languages 表中保存以下内容:

id |商店 ID |语言_id 4 4 1

并且 language_id = 1 不存在。

如果我在 create 操作中调试应用程序,我会得到以下信息:

"store"=>{"title"=>"asdasdsdsadasdasdasd", "description"=>"", "contact_email"=>"[email protected]", "logo"=>"", "store_languages_attributes"=>{"0"=>{"language_id"=>["3", "4"]}}}

You can see that the ids are right here: 3 and 4. So, I don't Know Why it saves 1.

Any ideas ?

I'm

I'm building a website on Ruby On Rails 3.0.7 and I want to save a store object and its languages. So, I have the following models:

class Store < ActiveRecord::Base
  belongs_to :user
  has_many :languages, :through => :store_languages
  has_many :store_languages

  accepts_nested_attributes_for :store_languages

  #Validations
  validates :title, :presence => true, :length => 5..100
  validates :contact_email, :presence => true, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }
end

class Language < ActiveRecord::Base
  has_many :stores, :through => :store_languages
  has_many :store_languages
end

class StoreLanguage < ActiveRecord::Base
  belongs_to :store
  belongs_to :language

  validates :store_id, :presence => true
  validates :language_id, :presence => true
end

StoresController's relevant actions:

  def new
    @store = Store.new
    @store.store_languages.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @store }
    end
  end

  # POST /stores
  # POST /stores.xml
  def create
    #raise params.inspect
    @store = current_user.stores.new(params[:store])

    respond_to do |format|
      if @store.save
        format.html { redirect_to(@store, :notice => 'Store was successfully created.') }
        format.xml  { render :xml => @store, :status => :created, :location => @store }
      else
        @store.store_languages.build
        format.html { render :action => "new" }
        format.xml  { render :xml => @store.errors, :status => :unprocessable_entity }
      end
    end
  end

View: /stores/new.html.erb:

<%= form_for(@store) do |f| %>
  <% if @store.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@store.errors.count, "error") %> prohibited this store from being saved:</h2>

      <ul>
      <% @store.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <label for="title">Title*</label><br />
    <%= f.text_field :title %>
  </p>

  <p>
    <label for="description">Description</label><br />
    <%= f.text_field :description %>
  </p>

  <p>
    <label for="contact_email">Contact E-mail*</label><br />
    <%= f.text_field :contact_email %>
  </p>

  <p>
    <label for="logo">Logo</label><br />
    <%= f.file_field :logo %>
  </p>

    <% f.fields_for :store_languages do |lf| %>

      <%= lf.collection_select :language_id, @languages, :id, :name, {}, {:multiple => true } %>
    <% end %>

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

So, I've got the following records in the languages table:

 id |   name
 3    English
 4    Español

What happens is that when I create a new store selecting the two languages from the list, it will save the following at the store_languages table:

id | store_id | language_id
4 4 1

And the language_id = 1 doesn't exist.

If I debug the application at the create action, I get the following:

"store"=>{"title"=>"asdasdsdsadasdasdasd", "description"=>"", "contact_email"=>"[email protected]", "logo"=>"", "store_languages_attributes"=>{"0"=>{"language_id"=>["3", "4"]}}}

You can see that the ids are correct here: 3 and 4. So, I don't know why it saves 1.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若有似无的小暗淡 2024-12-02 08:44:40

尝试

  <%= lf.collection_select :language_ids, @languages, :id, :name, {}, {:multiple => true } %>

(即在属性名称中使用 ids 而不是 id

Try

  <%= lf.collection_select :language_ids, @languages, :id, :name, {}, {:multiple => true } %>

(ie using ids instead of id in the attribute name)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文