Jquery Rails 3和ajax的部分功能
我有:devices.html.erbJquery Rails 3和ajax的部分功能,jquery,ajax,ruby-on-rails-3,Jquery,Ajax,Ruby On Rails 3,我有:devices.html.erb <p id="notice"><%= notice %></p> <p> <b>Kategorija:</b> <%= @device.category.name %> </p> <p> <b>Gamintojas:</b> <%= @device.manufacturer.name %> &l
<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)} %>
卡特戈里亚:
Gamintojas:
康库伦塔斯:
帕瓦迪尼马斯:
阿普雷斯马斯:
|
“规格值/规格值列表”,:locals=>{:specification\u values=>@device.specification\u values}%>
“规格值/新规格值”,:locals=>{:规格值=>SpecificationValue.new(:device\u id=>@device.id)}%>
有两个部分,第一个是规范值列表
列出规范值
装置
规范
价值
“你确定吗?”,:method=>:delete,:remote=>true%>
和:\新的\规格\数值
<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 %>
新规范\u值
正确)do | f |%>
禁止保存此规格值:
当我添加新的规范值时,我想在jquery的内部创建一个jquery,它将在部分列表中显示在devices.html.erb中的ajax中。谢谢。我使用带有以下代码的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 %>
这允许位置[number]设置“number”访问器。
我们在位置[number]后有[]的原因是允许向模型传递多个电话号码
此外,您需要先保存电话号码,然后再保存到位置模型中
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
我的模型位置也有很多:电话号码;这就解释了电话号码
现在请注意控制器:
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传递给render partial方法,Rails将自动生成多个电话号码
我希望这有帮助…=)
attr_accessor :numbers
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
def new
1.times { @location.phone_numbers.build }
@phone_numbers = @location.phone_numbers
@phone_number = @phone_numbers[0]
end