2012-02-02 36 views
0

我今天问了这个问题earlier,并从控制台,它按预期工作。但是,当我将表单构建为包含(例如,ajax,样式)时,它看起来并没有存储到模型中。模型保存在控制台中,而不是来自web ui

任何想法在哪里可以解决我的问题?

欢呼声 -

注册型号

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

尝试基本的调试打印。 “params”是什么? “@注册”是什么状态?该条件评估什么价值,等等? – 2012-02-02 04:47:38

+0

from our development.log file'参数:{“utf8”=>“✓”,“authenticity_token”=>“NPpMFeByG2U6 + yl8sE69lf6qI7HX/4GZmiCstBoB2AE =”,“注册”=> {“email_address”=>“hoop @ hype。 com“,”send_once“=>”0“,”send_any_time“=>”0“},”commit“=>”创建注册“} ' – rhodee 2012-02-02 04:49:59

回答

0

我觉得这条线是你的问题:

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

通过参数都通过模型去的时候,他们很可能true,不"1"。在Ruby和Rails中有一些非常细微的差别。

相关问题