2011-03-22 50 views
0

我有:devices.html.erb導軌3和Ajax在局部

<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)} %> 

用2分音,第一_specification_values_list

清單specification_values

<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 %> 

和:_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 %> 

我有jquery裏面,我想要做,當我添加新的規格值,它將apear在ajax這是在devices.html.erb部分列表中。謝謝。

回答

0

我呈現使用Ajax和下面的代碼部分:

在你的主文件中把這個:

<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 %> 

當你點擊「添加號碼」,該「_phone_number」將生成和加載在id爲 「PHONE_NUMBERS」

在我的部分股利我有:

<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> 

我有'位置[數字] []'的原因是因爲我在位置模型的視圖中使用_phone_number部分。我的位置模型有以下幾種:

attr_accessor :numbers 

這允許位置[數字]設置「數字」存取器。 我們在位置[數字]之後有[]的原因是允許將多個電話號碼傳遞給模型。

此外,您還需要保存的電話號碼before_save在選址模型

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 

我的模型位置也的has_many:PHONE_NUMBERS;這解釋了phone_numbers.build

現在到控制器:

def new 
    1.times { @location.phone_numbers.build } 
    @phone_numbers = @location.phone_numbers 
    @phone_number = @phone_numbers[0] 
end 

注意,在上面的代碼我用@phone_number,我傳遞給我的渲染部分。 Rails 3支持集合,這意味着,如果您希望默認創建多個電話號碼,則可以將@phone_numbers傳遞給呈現部分方法,並且rails會自動生成多個電話號碼!

我希望這有助於...... =)