I asked this question earlier today and from the console, it works as expected. However, when I built my form out to include (e.g. ajax, styles) it does not appear to be saving to the model.
Any ideas where to make my fix?
cheers –
Signup Model
class Signup < ActiveRecord::Base
attr_accessible :email_address, :send_once, :send_any_time
# email_regex = stub it out later
validates :email_address, presence: true,
#format: {with: email_regex},
uniqueness: {message: 'there can only be one you.'}
end
PagesController
class PagesController < ApplicationController
def index
@signup = Signup.new
end
def create
@signup = Signup.new(params[:signup])
if @signup.send_once == "1" or @signup.send_any_time == "1"
respond_to do |format|
if @signup.save
format.js
else
format.html {render action: :index}
end
end
else
#if they don't sign, do something!
end
end
end
__form.html.erb_
<%= form_for(@signup, method:post, as: :signup, url: pages_path, html: {id: "button"}, remote: true) do |f| %>
<% if @signup.errors.any? %>
<div id="error_explanation">
<p><%= pluralize(@signup.errors.count, "error") %> prohibited this post from being saved:</p>
<ul>
<% @signup.errors.full_messages.each do |user| %>
<li><%= user %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email_address %><br />
<%= f.email_field :email_address %>
<%= f.label :send_once %><br />
<%= f.check_box :send_once %>
<%= f.label :send_any_time %><br />
<%= f.check_box :send_any_time %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I think this line is your problem:
By the time the parameters have gone through the model, they’re probably
true, not"1". There’s a very subtle difference to these in Ruby and Rails.