Skip to content

Instantly share code, notes, and snippets.

@kirkbushell
Created August 15, 2011 00:06
Show Gist options
  • Save kirkbushell/1145485 to your computer and use it in GitHub Desktop.
Save kirkbushell/1145485 to your computer and use it in GitHub Desktop.
nested models problem
#models
class Link < ActiveRecord::Base
has_many :link_urls
accepts_nested_attributes_for :link_urls, :allow_destroy => true #, :reject_if => lambda { |a| a[:url].blank? }
end
class LinkUrl < ActiveRecord::Base
belongs_to :link
end
# _form
- @link.link_urls.build if @link.link_urls.empty?
- form_for [:admin, @link] do |f|
%ul.form
%li
= f.label :title
= f.text_field :title, :size => '50'
%li
= f.label :description
= f.text_area :description, :rows => 8, :cols => 70
%li
= label_tag 'Links'
.link-urls-container
= fields_for :link_urls do |uf|
.link-url-item
= uf.text_field :url, :size => '50'
= link_to 'x', nil, :onclick => 'return Admin.removeLink(this)'
%button{:onclick => 'Admin.addLink()', :type => 'button'} '+ Add link'
%li
= f.label :synonyms
= f.text_area :synonyms, :rows => 2, :cols => 70
%li
%label
= f.submit 'Save'
<form accept-charset="UTF-8" action="/admin/links/1" class="edit_link" id="edit_link_1" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="m0CsrjJxE+BLc7H6+6ph8szHOjmL7gYS2953hBCCzAY=" /></div>
<ul class='form'>
<li>
<label for="link_title">Title</label>
<input id="link_title" name="link[title]" size="50" type="text" value="Social media - Wikipedia, the free encyclopedia" />
</li>
<li>
<label for="link_description">Description</label>
<textarea cols="70" id="link_description" name="link[description]" rows="8">The term social media refers to the use of web-based and mobile technologies to turn communication into interactive dialogue. Social media are media for social interaction, as a superset beyond social communication, but mainly still communicating just interactively using ubiquitously accessible and scalable communication techniques.</textarea>
</li>
<li>
<label for="Links">Links</label>
<div class='link-urls-container'>
<div class='link-url-item'>
<input id="link_urls_url" name="link_urls[url]" size="50" type="text" />
<a href="/admin/links/1/edit" onclick="return Admin.removeLink(this)">x</a>
</div>
<button onclick='Admin.addLink()' type='button'>'+ Add link'</button>
</div>
</li>
<li>
<label for="link_synonyms">Synonyms</label>
<textarea cols="70" id="link_synonyms" name="link[synonyms]" rows="2"></textarea>
</li>
<li>
<label></label>
<input id="link_submit" name="commit" type="submit" value="Save" />
</li>
</ul>
</form>
@kirkbushell
Copy link
Author

  • Title
  • Description <textarea cols="70" id="link_description" name="link[description]" rows="8">The term social media refers to the use of web-based and mobile technologies to turn communication into interactive dialogue. Social media are media for social interaction, as a superset beyond social communication, but mainly still communicating just interactively using ubiquitously accessible and scalable communication techniques.</textarea>
  • Links
    x
    '+ Add link'
  • Synonyms <textarea cols="70" id="link_synonyms" name="link[synonyms]" rows="2"></textarea>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment