Diogo Wernik Diogo Wernik - 2 months ago 18
Ruby Question

passing array to f.select ruby on rails

On my rails application in the views/cards/_form i have the code for a f.select field:

<%= f.select :tag_speciality, options_for_select(@subdomain.tag_speciality), {}, {class: 'form-control'} %>


That give me the output:

<select class="form-control" name="card[tag_speciality]" id="card_tag_speciality">
<option value="Professor de Yoga">Professor de Yoga</option>
<option value="Professora de Yoga">Professora de Yoga</option>
<option value="Estúdio de Yoga">Estúdio de Yoga</option>
</select>


In my migration file i have:

add_column :cards, :tag_speciality, :string, array: true


When i go to the form and select any option, for example, "Professor de Yoga", and save, i get the result:

[]


instead of:

["Professor de Yoga"]


This is my controller:



class CardsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :update, :edit, :destroy, :favorite]
before_action :set_card, only: [:show, :edit, :update, :destroy, :favorite]
before_action :set_user, only: [:create]
before_action :set_domain_from_address, only: [:new, :create, :edit, :update]

# GET /cards
# GET /cards.json

def index
@cards = Card.all
end

def index_from_user
@cards = Card.where(user_id: params[:user_id])
render :index
end

def index_from_subdomain
@cards = Card.where(subdomain_id: params[:subdomain_id])
render :index
end

def index_from_domain
@cards = Card.where(subdomain_id: params[:subdomain_id])
render :index
end

# GET /cards/1
# GET /cards/1.json
def show
end

# GET /cards/new
def new
@card = Card.new_from_subdomain request.subdomain
end

# GET /cards/1/edit
def edit
end

# POST /cards
# POST /cards.json
def create
@card = @user.cards.new card_params

respond_to do |format|
if @card.save
format.html { redirect_to cards_from_subdomain_path(@subdomain.id), notice: 'Card was successfully created.' }
format.json { render :show, status: :created, location: @card }
else
format.html { render :new }
format.json { render json: @card.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /cards/1
# PATCH/PUT /cards/1.json
def update
respond_to do |format|
if @card.update(card_params)
format.html { redirect_to cards_from_subdomain_path(@subdomain.id), notice: 'Card was successfully updated.' }
format.json { render :show, status: :ok, location: @card }
else
format.html { render :edit }
format.json { render json: @card.errors, status: :unprocessable_entity }
end
end
end

# DELETE /cards/1
# DELETE /cards/1.json
def destroy
@card.destroy
respond_to do |format|
format.html { redirect_to cards_url, notice: 'Card was successfully destroyed.' }
format.json { head :no_content }
end
end

def favorite
current_user.toggle_favorite! @card.id
render nothing: true
end

private

def set_user
@user = current_user
end

# Use callbacks to share common setup or constraints between actions.
def set_card
@card = Card.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def card_params
params.require(:card).permit(
:name,
:phone,
:email,
:cover,
:is_published,
:subdomain_id,
:domain_id,
:profile_id,
:is_solidarity,
:tag_speciality,
:tag_location,
user_id: []
)
end
end





Is there anything that am i missing?
Thanks

Answer

Ditto @EJ2015.

Just add

@card.tag_speciality.push(card_params[:tag_speciality])
@card.save

to your create.

respond_to do |format|
      if @card.tag_speciality.push(card_params[:tag_speciality]) && @card.save
        format.html { redirect_to cards_from_subdomain_path(@subdomain.id), notice: 'Card was successfully created.' }
        format.json { render :show, status: :created, location: @card }
      else
        format.html { render :new }
        format.json { render json: @card.errors, status: :unprocessable_entity }
      end
    end