A
A
Artyom Lagun2013-12-01 20:13:54
Ruby on Rails
Artyom Lagun, 2013-12-01 20:13:54

How in RoR to save a field with type text into an array, and then select each word from this array and insert it into the form as a checkbox?

Hello. the question is: how do i save a text field into an array, and then select each word from this array and insert it into the form as a checkbox?
model:

class Category < ActiveRecord::Base
  belongs_to :section
  has_many :items
  attr_accessible :title, :section_id, :tags
  serialize :tags
  validates :title, :section_id, presence: true
end

controller:
def create
    @category = Category.new(category_params)

    if @category.save
      redirect_to admins_path, notice: 'Category create successful!'
    else
      @section = Section.order('title ASC')
      render :new
    end
  end

  def new
    @category = Category.new(:tags => Array.new)
    #@category.serializable_hash(@category.tags)
    @section = Section.order('title ASC')
  end
  private
  def category_params
    params.require(:category).permit(:title, :section_id, :tags => Array.new)
  end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanKiLL, 2013-12-01
@Holzfaller

Pass text to the method, return an array of words, cut the text by space.

def tag_l­ist text
  text.spl­it(" ")
end

in view like this, play around with the options in f.collection_select.
<%= f.label :tag_ids, "Теги", :class => 'css class' %>
<%= f.collection_select(:tag_ids Tag.all, :id, :name, {}, :class => 'css class') %>

Read more here collection_select

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question