Stefan Hansch
Stefan Hansch

Reputation: 1591

Dont display fields nested attributes with using gem cocoon

i have model poll with nested attributes 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

I installed gem cocoon:

next i create view new.html.haml:

%h1= title "Новый опрос"
= simple_form_for @poll do |f|
  = f.error_messages header_message: nil
  = f.input :question, disabled: [email protected]?(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

I open page new for creating new poll, but only field for new name quation poll and button "create poll". No fields for adding poll_items. I thinkm dont render form poll_items_fields.html.haml. But why? How fix?

Upvotes: 0

Views: 237

Answers (1)

Pavan
Pavan

Reputation: 33552

If I understood correctly, your new method should look like this

def new
  @poll = Poll.new
  @poll.poll_items.build
end

And also you have unswer instead of answer in poll_items_attributes, so fix that too.

def poll_params
  params.require(:poll).permit(:from_date, :question, :results_hidden, :to_date, :owner_id, poll_items_attributes: [:id, :answer, :_destroy])
end

Update:

You should also remove attr_accessible :from_date, :question, :results_hidden, :to_date, :owner_id, :poll_items_attributes from your Poll model as Rails 4 uses strong parameters.

And also this line @poll = Poll.new params[poll_params] in create method should look like this @poll = Poll.new(poll_params)

Upvotes: 2

Related Questions