rails link_to_add_fields不使用has_many:through添加字段(内部具有嵌套形式) [英] rails link_to_add_fields not adding fields with has_many :through (with nested form inside)

查看:42
本文介绍了rails link_to_add_fields不使用has_many:through添加字段(内部具有嵌套形式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的link_to_add_fields代码可用于许多其他模型,但是在这里不起作用.我使用Ryan Bates屏幕演员来模拟我的工作方式.

I have the link_to_add_fields code working for many other models, but here it is not working. I use Ryan Bates screen casts to model how I do mine.

可能与此不同的是,我在一个局部中有2个嵌套模型.表字段的一半来自模型a(期刊),另一半来自模型b(journal_entries),所有字段均基于租约模型.

What might different with this is that I have 2 nested models in one partial. Half the table fields are from model a (journals), and the other are from model b (journal_entries) and all are based off a Leases model.

该表如下所示,[xx]是输入字段.看起来有点像数据网格.

The table looks like the following, [xx] are input fields. It looks kinda like a data grid.

Dated      |Memo      |Account           |Credit     |
-----------------------------------------------------------
[a.dated]  |[a.memo]  |[b.account_name]  |[b.credit] |....
[a.dated]  |[a.memo]  |[b.account_name]  |[b.credit] |....
[a.dated]  |[a.memo]  |[b.account_name]  |[b.credit] |....
(Link:Click to add another row)

当我检查生成的html中的link_to_add_fields时,请参阅:

When I inspect the link_to_add_fields in the produced html is see:

<a href="#" class="add_fields" data-fields="&lt;fieldset style=&quot;
border:0px none;padding:0px .625em;&quot;&gt;  &lt;/fieldset&gt;" 
data-id="-629772378">+ Add transactions</a>

我希望采取的行动更多……显然,当我单击链接时,什么也没有发生.

I expected a lot more action than that... Obviously when I click the link, nothing happens.

_journals_form.html.erb

_journals_form.html.erb

<div>
  <%= f.fields_for :journals do |journals| %>
    <%= render "journal_fields" , {f: journals} %>
  <% end %>
  <%= link_to_add_fields "+ Add transactions", f, :journals %>
</div>

_journal_fields.html.erb,请注意嵌套的f.fields_for以及f.attribute和g.attribute的混合

_journal_fields.html.erb, Notice the nested f.fields_for and the mix of f.attribute and g.attribute

<fieldset style="border:0px none;padding:0px .625em;">
  <%= f.fields_for :journal_entries do |g| %> 
      <table>
        <tr>
          <td style="width:200px;font-size:.7em"><%= f.date_select :dated %></td>
          ...
          <td style="width:100px;"><%= g.text_field :account_name, size: 8 %></td>
          <td style="width:100px;"><%= g.hidden_field :_destroy %><%= link_to "remove", '#', class: "remove_fields" %></td>
        </tr>
      </table>
  <% end %>
</fieldset>

我的控制器似乎可以编辑/更新并添加两个模型(journals和journal_entries)的行,除了remove链接有问题...

My controller appears to edit/update and add rows of both models (journals and journal_entries) except that the remove link is buggy...

  def edit
    @lease = Lease.find(params[:id])
    @lease.tenants.build
    1.times do 
      journal = @lease.journals.build
      journal.journal_entries.build
    end
  end
  def update
    @lease = Lease.find(params[:id])
    if @lease.update_attributes(params[:lease])
      redirect_to edit_pm_lease_path(@lease), notice: 'Lease was successfully updated.'
    else
      render action: "edit"
    end
  end

该模型基于租赁".租赁有许多日记,以及通过日记的许多日记条目.这就是我们跟踪给定租约的付款和账单的方式.租赁也有许多租户(这个add_fields ...很好用).

The model is based off Leases. Leases have many journals and many journal entries through journals. This is how we keep track of payments and bills for a given lease. Leases also has many tenants (and this add_fields... works great).

Lease.rb

class Lease < ActiveRecord::Base
  ...
  has_many :tenants
  has_many :journals, :order => [:dated, :id] #, :conditions => "journals.lease_id = id"
  has_many :journal_entries, :through => :journals  
  accepts_nested_attributes_for :tenants , :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true 
  accepts_nested_attributes_for :journal_entries , :allow_destroy => true
  accepts_nested_attributes_for :journals ,  :allow_destroy => true    
  #Below didn't seem to help or hinder so it's removed.
    #accepts_nested_attributes_for :journals, :journal_entries, :allow_destroy => true 
  ...
end

journal.rb

journal.rb

class Journal < ActiveRecord::Base
  ... 
  belongs_to :lease, :conditions =>  :lease_id != nil   
  has_many :journal_entries
  accepts_nested_attributes_for :journal_entries , :allow_destroy => true
end

journal_entry.rb

journal_entry.rb

class JournalEntry < ActiveRecord::Base
  belongs_to :journal
end

link_to_add_fields帮助器:(来自Ryan bates railscasts的参考(第EPISODE#196)

link_to_add_fields helper: (References from Ryan bates railscasts (EPISODE #196)

module ApplicationHelper
  def external_link_to(label, target, options = [])
    #length = 25 #options[:length] ||= 25
    #window = options[:target] ||= "new"
    unless target.downcase.start_with?("http://","https://")
      link = "http://" + target.strip
    end
    link_to target, link, :target => "new"   
  end

  def link_to_add_fields(name, f, association)
    new_object = f.object.send(association).klass.new
    id = new_object.object_id
    fields = f.fields_for(association, new_object, child_index: id) do |builder|
      render(association.to_s.singularize + "_fields", f: builder)
    end
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
  end
end

不确定我在哪里出错了,数据的添加和删除正在工作,但是仅journal/journal_entry表单的link_to_add_fields没有响应.我没有使用nested_form gem,如果有所不同...

Not sure where I went wrong with this one, the adding and removing of the data are working, but the link_to_add_fields for only the journal/journal_entry form is not responding as it should. I'm not using the nested_form gem, if that makes a difference...

我正在使用Rails 3.2.12,Ruby 1.9.3

I'm using Rails 3.2.12, Ruby 1.9.3

任何帮助将不胜感激.

推荐答案

通过删除fields_for代码博客来修改journal_fields.hrml.erb文件.用于为Lease类添加嵌套的将军.

Modify your journal_fields.hrml.erb file with removing fields_for code blog. for add nested generals for Lease class.

 <fieldset style="border:0px none;padding:0px .625em;">
     <table>
        <tr>
          <td style="width:200px;font-size:.7em"><%= f.date_select :dated %></td>
          # Your form field
          <td style="width:100px;"><%= g.text_field :account_name, size: 8 %></td>
          <td style="width:100px;"><%= g.hidden_field :_destroy %><%= link_to "remove", '#', class: "remove_fields" %></td>
        </tr>
      </table>
  </fieldset>

这篇关于rails link_to_add_fields不使用has_many:through添加字段(内部具有嵌套形式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆