Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:23:19+00:00 2026-06-15T09:23:19+00:00

The problem I have is the select box in my view does not maintain

  • 0

The problem I have is the select box in my view does not maintain the value selected if a validation error occurs.

I have business and category relationship which has a many to many association

category.rb

class Category < ActiveRecord::Base
    has_and_belongs_to_many :businesses

business.rb

class Business < ActiveRecord::Base
    has_and_belongs_to_many :categories

In my view I have a select field

<%= f.select(:category_tokens, Category.all.collect {|c| [ c.name, c.id ] }, { :include_blank => "Select Category", :selected => params['category'] }}) %>

I can access the business categories in the console using business.categories which returns and array.

In my view to troubleshoot the params I’ve added.

<%= @business.categories %>
<%= @business.attributes.inspect %>
<%= @business.user.attributes.inspect %>

The output for these shows provides the following

[#<Category id: 2, name: "kitchen renovations", created_at: "2012-10-19 14:16:52", updated_at: "2012-10-19 14:16:52">]

{"id"=>nil, "business_name"=>"", ... additional attributes}

{"id"=>nil, "email"=>"", ... additional attributes}

The params hash looks like this

Processing by BusinessesController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"fVz1mJZI8QT/HYKRph8YTvzRec0IVV0RS55v5QD5SL0=", 
"business"=>{"category_tokens"=>"2", 
"location"=>"", "service_area"=>"20", 
"business_name"=>"", "abn_number"=>"", 
"business_description"=>"", 
"address"=>"", "suburb"=>"", 
"notification_type"=>"", 
"user_attributes"=>{"first_name"=>"", "phone"=>"", "email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}, "commit"=>"Sign up my Business"}

So the category is being set but I’m not sure how to add this to my select in the view to use the category as the select value when a validation error occurs.

EDIT — adding controller code —-

class BusinessesController < ApplicationController

  def new
    @business = Business.new
    @business.build_user
  end

  def create
    @business = Business.new(params[:business])
     respond_to do |format|
      if @business.save
         cookies[:auth_token] = @business.user.auth_token
      format.html { redirect_to jobs_users_path, notice: 'Your business was successfully created.' }
   else
     format.html { render action: "new" }
   end
 end

end

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:23:20+00:00Added an answer on June 15, 2026 at 9:23 am

    I fixed this issue by making a few simple changes.

    in my controller I added the following

    def create
      #...
      category = (params[:business][:category_tokens])
      @category = category
      #...
    end
    

    Then used this in my view for the selected option.

    <%= f.select(:category_tokens, Category.all.collect {|c| [ c.name, c.id ] }, { :include_blank => "Select Category", :selected => @category }}) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with building multi level select box. I have category table
I have a problem with an SQL query. SELECT SUM(table_colum) AS value, SUM(value *
I have next problem, when I'm trying to retrieve value from xforms:select elements I
(Motivation: Consider a problem where you have to select a sports team from a
I have problem while loading data into html select when users press or click
i have problem using LIKE structure in DB2 : for example: select * from
i have a problem concerning a select query in MYSQL i have two different
I have a problem with adding OPTIONS to SELECT tag dynamically. Here is my
I have a problem with the query below in postgres SELECT u.username,l.description,l.ip,SUBSTRING(l.createdate,0,11) as createdate,l.action
I have the problem, that MSSQL Server 2000 should select some distinct values from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.