Форма для частичного отображения отлично работает и работает на странице показа Продукта, но когда я добавляю частичное к каждому элементу на странице индекса продукта, отображается содержимое формы, но оно находится вне тегов (Форма состоит из переключателей для форма в стиле рейтинговой системы (без ajax - работает с кнопкой отправки). Правильные радиокнопки выбраны там, где они существуют, и отправка, кажется, все еще работает правильно).
Самое странное в этом - (как в Chrome, так и в Firefox), когда я просматриваю источник, контент находится внутри тегов, но когда я использую «проверять элемент», контент находится вне тегов. Это проблема, потому что мои css и jquery не могут найти содержимое в элементе формы (элемент находится на странице показа).
Может кто-нибудь объяснить, почему это происходит и как я могу это исправить?
Подробности ниже:
Рубин: 1.9.3p0
Рельсы: 3.1.3
У меня есть три модели: User, Product и TasteNote (внешний вид, аромат, вкус, текстура, пригодность для питья и текстовая область для комментариев).
Маршруты:
Appname::Application.routes.draw do
resources :users
resources :products
resources :taste_notes
Модели:
class User < ActiveRecord::Base
has_many :taste_notes, :foreign_key => :user_id, :dependent => :destroy
class Product < ActiveRecord::Base
has_many :taste_notes, :foreign_key => :product_id, :dependent => :destroy
class TasteNote < ActiveRecord::Base
attr_accessible :product_id, :appearance, :aroma, :taste,
:texture, :drinkability, :comments
belongs_to :user
belongs_to :product
validates :user_id, :presence => true
validates :product_id, :presence => true
Контроллер продуктов:
def show
..
...
if @taste_note = current_user.taste_notes.where(:product_id => @product.id).first
else
@taste_note = TasteNote.new(:product_id => @product.id)
end
end
Продукт Показать страницу вызова к частичному:
..
...
<%= render :partial => "tnote", :locals => {:product => @product, :taste_note => @taste_note} %>
...
..
Страница индекса продукта:
..
...
<% get_tasting_note(product) %> <!-- This returns @taste_note -->
<%= render :partial => "tnote", :locals => {:product => product, :taste_note => @taste_note} %>
...
..
product_helper.rb
def get_tasting_note(product)
if @taste_note = current_user.taste_notes.where(:product_id => product.id).first
@taste_note
else
@taste_note = current_user.taste_notes.build(:product_id => product.id)
end
end
Частичное примечание вкуса (_tnote.html.erb):
<div id="taste_note">
<hr>
<h3>Tasting Notes:</h3>
<%= form_for taste_note, :html => { :class => "rating_ballot_index", :id => "edit_taste_note_#{taste_note.id}" } do |f| %>
<% [ 'appearance', 'aroma', 'taste', 'texture', 'drinkability' ].each do |attribute| %>
<br><br><%= attribute.humanize %><br>
<% (1..5).each do |level| %>
<%= f.label("#{attribute}_#{level}_#{product.id}", content_tag(:span, level), :class=>"taste_note #{attribute}") %>
<%= radio_button_tag("taste_note[#{attribute}]", level, current_user_taste_note("#{attribute}", product.id) == level, :class => "taste_note_button #{attribute}", :id => "taste_note_#{attribute}_#{level}_#{product.id}") %>
<% end %>
<% end %>
<br><br>
<%= f.label :comments %><br><br>
<%= f.text_area :comments, :rows => 5 %>
<br>
<%= f.submit "submit" %>
<% end %>
</div>
product_helper.rb - current_user_taste_note ()
# Show page gets product id from params[:id]
# Index page can not, so pass in productid parameter
def current_user_taste_note(value, productid = params[:id])
if value == "appearance"
if @taste_note = current_user.taste_notes.find_by_product_id(productid)
@taste_note.appearance
else
"N/A"
end
elsif value == "aroma"
if @taste_note = current_user.taste_notes.find_by_product_id(productid)
@taste_note.aroma
else
"N/A"
end
elsif value == "taste"
if @taste_note = current_user.taste_notes.find_by_product_id(productid)
@taste_note.taste
else
"N/A"
end
elsif value == "texture"
if @taste_note = current_user.taste_notes.find_by_product_id(productid)
@taste_note.texture
else
"N/A"
end
elsif value == "drinkability"
if @taste_note = current_user.taste_notes.find_by_product_id(productid)
@taste_note.drinkability
else
"N/A"
end
end
end