i have: devices.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Kategorija:</b>
<%= @device.category.name %>
</p>
<p>
<b>Gamintojas:</b>
<%= @device.manufacturer.name %>
</p>
<p>
<b>Konkurentas:</b>
<%= @device.competitor.name %>
</p>
<p>
<b>Pavadinimas:</b>
<%= @device.name %>
</p>
<p>
<b>Aprašymas:</b>
<%= @device.description %>
</p>
<%= link_to 'Redaguoti', edit_device_path(@device) %> |
<%= link_to 'Atgal', devices_path %>
<div id= "specification_value">
<%= render :partial => "specification_values/specification_values_list", :locals => {:specification_values=>@device.specification_values} %>
</div>
<%= render :partial => "specification_values/new_specification_value", :locals=>{:specification_value=>SpecificationValue.new(:device_id=>@device.id)} %>
with 2 partials, first _specification_values_list
<table>
<tr>
<th>Device</th>
<th>Specification</th>
<th>Value</th>
<th></th>
<th></th>
<th></th>
</tr>
<% specification_values.each do |specification_value| %>
<tr>
<td><%= specification_value.device_id %></td>
<td><%= specification_value.specification_id %></td>
<td><%= specification_value.value %></td>
<td><%= link_to 'Show', specification_value %></td>
<td><%= link_to 'Edit', edit_specification_value_path(specification_value) %></td>
<td><%= link_to 'Destroy', specification_value, :confirm => 'Are you sure?', :method => :delete, :remote=>true %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Specification value', new_specification_value_path %>
and: _new_specification_value
<h1>New specification_value</h1>
<%= form_for(specification_value, :remote => true) do |f| %>
<% if specification_value.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(specification_value.errors.count, "error") %> prohibited this specification_value from being saved:</h2>
<ul>
<% specification_value.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :device_id %><br />
<%= f.text_field :device_id %>
</div>
<div class="field">
<%= f.label :specification_id %><br />
<%= f.text_field :specification_id %>
</div>
<div class="field">
<%= f.label :value %><br />
<%= f.text_field :value %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Back', specification_values_path %>
I have jquery inside, i want to make, when i add new specification_value that it will apear in partial list with ajax which is in devices.html.erb. Thank you.
I render a partial using ajax with the following code:
In your main file put this:
<div id="phone_numbers">
<%= render @phone_number, :phone_types => PhoneNumbersHelper::phone_types %>
</div>
<%= link_to_function "Add Number", do |page|
partial = escape_javascript( render PhoneNumber.new, :phone_types => PhoneNumbersHelper::phone_types )
page << "$('#phone_numbers').append(\"#{partial}\")"
end %>
When you click "Add number", the "_phone_number" will be generated and loaded in the div with the id "phone_numbers"
In my partial I have:
<div class="phone_number">
<%= fields_for 'location[numbers][]', phone_number do |f| %>
<div class="field">
<%= f.label "Phone Number" %><br/>
+(<%= f.text_field :code, :class => 'phone_code' %>)-
<%= f.text_field :number, :class => 'phone_number' %>
<%= f.select :ptype, phone_types %>
</div>
<% end%>
</div>
The reason I have 'location[numbers][]' is because I'm using the _phone_number partial in a view for the Location model. My Location model has the following:
attr_accessor :numbers
This allows location[numbers] to set the "numbers" accessor. The reason we have [] after location[numbers] is to allow passing multiple phone numbers to the model.
Also, you need to save the phone numbers before_save in the Location model
before_save :save_numbers
def save_numbers
unless numbers.nil?
numbers.each do |n|
unless n[:number].nil? || n[:code].nil? || n[:number].blank? || n[:code].blank?
phone_numbers.build(n)
end
end
end
end
My model location also has_many :phone_numbers ; this explains the phone_numbers.build
Now to the controller:
def new
1.times { @location.phone_numbers.build }
@phone_numbers = @location.phone_numbers
@phone_number = @phone_numbers[0]
end
Note that in the code above I used @phone_number , which I pass to my render partial. Rails 3 supports collections which means, if you want many phone numbers created by default, you can pass @phone_numbers to the render partial method and rails will automatically generate multiple phone numbers!
I hope this helps... =)