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

  • Home
  • SEARCH
  • 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 6942919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:05:18+00:00 2026-05-27T13:05:18+00:00

I have no idea why this isn’t working, but I can’t seem to get

  • 0

I have no idea why this isn’t working, but I can’t seem to get a complex form to create my necessary join models.

So a site can have many environments, through siteenvs.

site.rb

class Site < ActiveRecord::Base
  has_many :siteenvs, :dependent => :destroy
  has_many :environments, :through => :siteenvs, :uniq => true
  attr_accessible :name, :description, :documentation, :protocol, :port, :siteenvs_attributes
  accepts_nested_attributes_for :siteenvs, :reject_if => proc { |attributes| attributes["environment_id"] == "0"}
end

siteenv.rb

class Siteenv < ActiveRecord::Base
  belongs_to :site
  belongs_to :environment
  validates :site_id, :uniqueness => { :scope => :environment_id }
end

environment.rb

class Environment < ActiveRecord::Base
  has_many :siteenvs, :dependent => :destroy
  has_many :sites, :through => :siteenvs, :uniq => true
end

Pretty standard stuff here. Now here is my controller. What I’m doing is asking the database how many Environments there are, and building that many siteenvs for a new site. This is so that the form will have one checkbox for each environment, so a user can choose whatever they would like.

sites_controller.rb

class SitesController < ApplicationController
  load_and_authorize_resource
  respond_to :html, :xml, :json

  def new 
    @site = Site.new
    Environment.all.each do |env|
      siteenv = @site.siteenvs.build(:environment_id => env.id)
    end 
    respond_with (@site)
  end

  ...

  def create
    @site = Site.new(params[:site])
    if @site.save
      flash[:success] = "New site created."
      respond_with(@site, :location => @site)
    else
      render 'new'
    end
  end
end

And then my new site form looks like this:

sites/new.html.erb

<h1>Create a new site</h1>

<%= form_for(@site) do |f| %>
  <h3>Step 1 - General</h3>
    <table>
      <%= render 'site_fields', :f => f %>
    </table>

  <h3>Step 2 - Environments</h3>
    <div id="environments">
      <ul>
        <%= f.fields_for :siteenvs do |builder| %>
          <% environment = Environment.find(builder.object.environment_id) %>
          <li><%= builder.check_box :environment_id, { :checked => false, :class => "s_environment_checkbox", :env => environment.name, :domain => environment.domain }, builder.object.environment_id %>
          <%= builder.label :environment_id, "#{environment.name}" %>
          </li>
        <% end %>
      </ul>
    </div>

    <div class="actions">
      <%= f.submit "Finish" %>
    </div>
<% end %>

So what happens is that my form renders correctly, and all the HTML attributes appear to be correct. The value of the checkboxes is the ID of the corresponding environment, and the names all appear to be happy Rails generated HTML:

form HTML

<li>
    <input name="site[siteenvs_attributes][0][environment_id]" type="hidden" value="0">
    <input class="s_environment_checkbox" domain="example.com" env="env1" id="site_siteenvs_attributes_0_environment_id" name="site[siteenvs_attributes][0][environment_id]" type="checkbox" value="1">
</li>

However, unless you mark every single checkbox, I get an error referring to some method on the siteenvs model. I imagine this is due to the fact that submitted parameters are referencing an environment_id that doesn’t exist (0), but I would think that rails would ignore this. In fact, I’m not even sure why those environment_ids are being submitted if they aren’t checked! Here is some example output from when I only chose one checkbox, out of 5:

submitted params

{"utf8"=>"✓",
"authenticity_token"=>"Q1zdbb/ibcJrKb2FYf45o+Q43o2PZm1UU75dngis1UE=",
"site"=>{"name"=>"bob",
"description"=>"",
"documentation"=>"",
"protocol"=>"http",
"port"=>"80",
"siteenvs_attributes"=>{"0"=>{"environment_id"=>"1",
"url"=>""},
"1"=>{"environment_id"=>"0"},
"2"=>{"environment_id"=>"0"},
"3"=>{"environment_id"=>"0"},
"4"=>{"environment_id"=>"0"}}},
"commit"=>"Finish"}

So I have no idea where that 0 came from, nor why the unselected checkboxes are even being including in the params. Any ideas?

  • 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-05-27T13:05:19+00:00Added an answer on May 27, 2026 at 1:05 pm

    This is straight from the documentation for the check_box method for forms:

    Returns a checkbox tag tailored for accessing a specified attribute
    (identified by method) on an object assigned to the template
    (identified by object). This object must be an instance object
    (@object) and not a local object. It’s intended that method returns an
    integer and if that integer is above zero, then the checkbox is
    checked. Additional options on the input tag can be passed as a hash
    with options. The checked_value defaults to 1 while the default
    unchecked_value is set to 0 which is convenient for boolean values.

    Gotcha

    The HTML specification says unchecked check boxes are not successful,
    and thus web browsers do not send them. Unfortunately this introduces
    a gotcha: if an Invoice model has a paid flag, and in the form that
    edits a paid invoice the user unchecks its check box, no paid
    parameter is sent. So, any mass-assignment idiom like

    @invoice.update_attributes(params[:invoice])

    wouldn’t update the flag.

    From the params of your code I see that the four environment_id => "0" are the unchecked value of the checkbox. You will want to filter out those when you handle the data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no idea why this code isn't working, but hopefully the fix is
I have no idea why this isn't working. I've taken code from a previous
OK, so I have no idea why this isn't working. I've found 4 different
Anyone have any idea why this isn't working? $(function(){ console.log('ready'); $.ajax({ dataType : 'jsonp',
Anybody have any idea why this query isn't working? $result = mysql_query(SELECT * FROM
I have no idea why this isn't working. NSData *data = [NSData dataWithContentsOfURL:place.iconData]; UIImage
okay, i have no idea why this isn't working at all. it seems like
I have no idea if this is possible, but if it is, what would
I have no idea why this doesn't work, but doing some validation functions and
I have no idea if this is possible ... but it would be cool.

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.