模型保存在控制台中,而不是来自web ui

时间:2012-02-02 04:42:59

标签: ruby-on-rails

我今天问了这个问题earlier,从控制台,它按预期工作。但是,当我构建我的表单以包含(例如ajax,样式)时,它似乎没有保存到模型中。

有什么想法可以解决这个问题吗?

欢呼 -

注册模型

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 %>

1 个答案:

答案 0 :(得分:0)

我认为这一行是你的问题:

if @signup.send_once == "1" or @signup.send_any_time == "1"

当参数通过模型时,它们可能是true,而不是"1"。 Ruby和Rails中有一些非常微妙的区别。