Answer the question
In order to leave comments, you need to log in
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
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
Pass text to the method, return an array of words, cut the text by space.
def tag_list text
text.split(" ")
end
f.collection_select
.<%= f.label :tag_ids, "Теги", :class => 'css class' %>
<%= f.collection_select(:tag_ids Tag.all, :id, :name, {}, :class => 'css class') %>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question