Skip to content

Instantly share code, notes, and snippets.

@willsza
Last active August 29, 2015 14:26
Show Gist options
  • Save willsza/68e7f36a77c868a5bc31 to your computer and use it in GitHub Desktop.
Save willsza/68e7f36a77c868a5bc31 to your computer and use it in GitHub Desktop.
Rails 4 - Salvar Array de Selects
= simple_form_for @page do |f|
= f.error_notification
.form-inputs
= f.input :title
= f.association :section
.panel.panel-default
.panel-heading
.panel-title Definir direcionamento da página
.panel-body
.form-inline
.form-group
= f.input :ensuing, label: false, collection: [ "Enviar para", "Objetivo", "Pergunta", "Tag" ], include_blank: false, :input_html => { :id => "first_select", :name => 'page[ensuing][]' }
.form-group
= f.input :ensuing, label: false, collection: Question.order(:title), include_blank: false, prompt: "Selecione a pergunta", :input_html => { :id => "question_select", :name => 'page[ensuing][]' }
.form-group
= f.input :ensuing, label: false, collection: Question.order(:title), as: :grouped_select, as: :grouped_select, group_method: :answer_options, label_method: :content, value_method: :content, include_blank: false, :input_html => { :id => "answer_option_select", :name => 'page[ensuing][]' }
.form-group
= f.input :ensuing, label: false, collection: Page.order(:title), include_blank: false, prompt: "Enviar para", :input_html => { :id => "next_select", :name => 'page[ensuing][]' }
.form-actions
= f.button :submit, class: 'btn btn-success'
class Page < ActiveRecord::Base
belongs_to :section
has_many :questions
has_many :posts
has_one :previous, :class_name => "Page", :foreign_key => "next_id"
belongs_to :ensuing, :class_name => "Page"
serialize :ensuing, Array if Rails.env.development?
end
class PagesController < ApplicationController
before_action :set_page, only: [:show, :edit, :update, :destroy]
# POST /pages
# POST /pages.json
def create
@page = Page.new(page_params)
respond_to do |format|
if @page.save
format.html { redirect_to @page, notice: 'Page was successfully created.' }
format.json { render :show, status: :created, location: @page }
else
format.html { render :new }
format.json { render json: @page.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_page
@page = Page.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def page_params
params.require(:page).permit(:title, :section_id, ensuings: [])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment