我使用嵌套属性poll_items进行模型轮询:
class Poll < ActiveRecord::Base
ITEMS_COUNT_MAX = 5
attr_accessible :from_date, :question, :results_hidden, :to_date, :owner_id, :poll_items_attributes
belongs_to :owner, polymorphic: true
has_many :poll_items, dependent: :destroy
has_many :poll_votes, through: :poll_items
accepts_nested_attributes_for :poll_items, allow_destroy: true,
reject_if: proc { |attributes| attributes['answer'].blank? }
#validates :owner, :question, :from_date, :to_date, presence: true
#validate :validate_duplicate, on: :create
validate :validate_max_poll_items, on: :update
after_initialize :defaults
...................................................................
model pollItem
attr_accessible :answer
attr_readonly :poll_votes_count
belongs_to :poll
has_many :poll_votes, dependent: :destroy
has_many :users, through: :poll_votes
validates :poll, :answer, presence: true
validate :validate_duplicate, on: :create
scope :editable, lambda { |u|
unless u.moderator?
where(:poll.owner.user_id => u.id).where(:created_at.gt Settings.edit_delay.minutes.ago)
end
}
private
def validate_duplicate
errors.add(:base, :duplicate) unless poll.poll_items.where(answer: answer).empty?
end
end
Poll_controller:
class PollsController < ApplicationController
before_filter :authenticate_user!
before_filter :set_poll, only: [:show, :edit, :update, :destroy]
before_filter :find_owner, only: [:new, :create]
def new
@poll = Poll.new
end
def create
@poll = Poll.new params[poll_params]
#@poll.owner = @owner
respond_to do |format|
if @poll.save
format.html { redirect_to [:edit, @poll], notice: 'Опрос был успешно создан.' }
else
format.html { render :new }
end
end
end
def show
end
def edit
@poll.poll_items.build
end
def update
if @poll.editable?(current_user)
if @poll.update_attributes params[:poll]
respond_to do |format|
format.html { redirect_to [:edit, @poll], notice: 'Опрос был успешно обновлен.' }
end
else
respond_to do |format|
format.html { render :edit, alert: @poll.errors }
end
end
end
end
def destroy
@poll.destroy
respond_to do |format|
format.html { redirect_to @owner, notice: 'Опрос был успешно удален.' }
end
end
private
def poll_params
params.require(:poll).permit(:from_date, :question, :results_hidden, :to_date,
:owner_id, poll_items_attributes: [:id, :unswer, :_destroy])
end
protected
def set_poll
@poll = Poll.find(params[:id])
@owner = @poll.owner
end
def find_owner
@owner = case
when params[:post_id]
Post.visible(current_user).find(params[:post_id])
when params[:blog_post_id]
BlogPost.with_privacy(current_user).find(params[:blog_post_id])
end
end
end
我安装了gem cocoon:
接下来我创建视图new.html.haml:
%h1= title "Новый опрос"
= simple_form_for @poll do |f|
= f.error_messages header_message: nil
= f.input :question, disabled: !@poll.editable?(current_user), input_html: { class: 'input-block-level' }
= f.input :results_hidden, as: :boolean, inline_label: 'Скрыть результаты до окончания опроса', label: false
= f.input :from_date, as: :datetime, input_html: { class: 'poll_date' }
= f.input :to_date, as: :datetime, input_html: { class: 'poll_date' }
.items-index
%h3#poll-items Варианты ответа (не больше пяти)
= f.simple_fields_for :poll_items do |poll|
= render 'poll_items_fields', f: poll
= link_to_add_association 'Добавить еще вариант', f, :poll_items
.form-actions
= f.button :submit, 'Опубликовать опрос', class: 'btn-bg'
%p
Вернуться к посту:
= link_to @owner
poll_items_fields.html.haml:
.poll_row
.poll_item
= f.input :answer, input_html: { class: 'ctrlenter expanding' }, label: false, placeholder: 'Введите вариант ответа'
= link_to_remove_association "удалить", f
我打开新页面用于创建新的民意调查,但只有新名称quation民意调查和按钮&#34;创建民意调查&#34;。没有用于添加poll_items的字段。我认为不要渲染表单poll_items_fields.html.haml。但为什么?如何解决?
答案 0 :(得分:2)
如果我理解正确,您的new
方法应如下所示
def new
@poll = Poll.new
@poll.poll_items.build
end
而且unswer
中的answer
代替poll_items_attributes
,所以也要解决这个问题。
def poll_params
params.require(:poll).permit(:from_date, :question, :results_hidden, :to_date, :owner_id, poll_items_attributes: [:id, :answer, :_destroy])
end
<强> 更新 强>
您还应该从attr_accessible :from_date, :question, :results_hidden, :to_date, :owner_id, :poll_items_attributes
模型中删除Poll
Rails 4 使用 强参数 。
@poll = Poll.new params[poll_params]
方法中的这一行create
应该如下@poll = Poll.new(poll_params)