复选框选择和表单对象不保存 [英] Checkbox selection and form object do not save

查看:114
本文介绍了复选框选择和表单对象不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中包含:




  • 两个复选框

  • 地址



我不知道这是最好的设计,但是模型包含三个属性:




  • email_name(类型字符串)

  • send_once(type boolean)

  • send_anytime )



我想保存电子邮件地址,如果两个复选框之一填充,否则不保存任何内容。 >

一切似乎都按照我的预期工作,但是一旦我看到了rails控制台,注册模型是空的。



我的日志文件说:

 在2012-02-01 16:34发布127.0.0.1的POST/ pages :55 -0500 
由PagesController处理#创建为HTML
参数:{utf8=>✓,authenticity_token=>WChW / OmDPS2Td1D3x / 36aCJj7V75FdiAPJ9AQvtAIS4 =,post => {email_address=>email@examp.le,send_once=>0,send_any_time=>1},commit=>创建注册}
(0.3ms)BEGIN
(0.5ms)SELECT 1 FROMsignupsWHEREsignups。email_address=''LIMIT 1
(0.3ms)ROLLBACK
pages / _form.html.erb(3.7ms)

如何解决这个问题?目前模式不保存。



注意:我创建了一个注册 code>对象在Pages控制器(think:newsletter)中。



$ b

  class PagesController< ApplicationController 
def index
@signup = Signup.new
end

def create
@signup = Signup.new(params [:signup])
respond_to do | format |
if @ signup.save
format.html
#format.js
else
format.html {render action::index}
end
end
end
end

  class Signup< ActiveRecord :: Base 
attr_accessible:email_address
#email_regex =提出一些东西
validates:email_address,presence:true,
#format:{with:email_regex},唯一性:消息:只有一个你。}
end

_ form.html.erb



 <%= form_for(@signup,as::post,url:pages_path )do | f | %> 
<%if @ signup.errors.any? %>
< div id =error_explanation>
< p><%= pluralize(@ signup.errors.count,error)%>禁止保存此信息:< / 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%>


解决方案

您的参数正在传入:post和your create语句正在寻找params [:signup]。您需要更改控制器以查找params [:post]或更改您的表单以提交到params [:signup]。

 <%= form_for(@signup,as::signup,url:pages_path)do | f | %> 

您还需要添加一个检查复选框是否被选中的检查。
假设您的参数是:

 signup=> {email_address=>email @ examp .le,send_once=>0,send_any_time=>1} 

您需要:

  class PagesController< ApplicationController 
def index
@signup = Signup.new
end

def create
@signup = Signup.new(params [:signup])
if @ signup.send_once ==1or @ signup.send_any_time ==1then
respond_to do | format |
if @ signup.save
format.html
else
format.html {render action::index}
end
end
else
#如果他们没有注册,你想做什么?重定向到什么地方?
end
end
end


I have a form that includes:

  • two checkboxes
  • text field to gather email addresses

I am not sure if this is the best design, but the model contains three attributes:

  • email_name (type string)
  • send_once (type boolean)
  • send_anytime (type boolean)

I would like to save the email address if either of the two checkboxes are filled otherwise don't save anything.

Everything appeared to be working as I expected, but once I looked inside the rails console, the signup model was empty.

My log file says this:

Started POST "/pages" for 127.0.0.1 at 2012-02-01 16:34:55 -0500
Processing by PagesController#create as HTML
Parameters: {"utf8"=>"✓",  "authenticity_token"=>"WChW/OmDPS2Td1D3x/36aCJj7V75FdiAPJ9AQvtAIS4=", "post"=>{"email_address"=>"email@examp.le", "send_once"=>"0", "send_any_time"=>"1"}, "commit"=>"Create Signup"}
(0.3ms)  BEGIN
(0.5ms)  SELECT 1 FROM "signups" WHERE "signups"."email_address" = '' LIMIT 1
(0.3ms)  ROLLBACK
Rendered pages/_form.html.erb (3.7ms)

How do I go about this? Currently the model does not save. So, specifically what should the model and the controller look like?

NOTE: I am creating a signup object inside of the Pages controller (think: newsletter).

Controller

class PagesController < ApplicationController
 def index
  @signup = Signup.new
 end

def create
 @signup = Signup.new(params[:signup])
 respond_to do |format| 
  if @signup.save
    format.html
    #format.js
  else
    format.html {render action: :index}
   end
  end
 end
end

Model

class Signup < ActiveRecord::Base
  attr_accessible :email_address
  # email_regex = come up with something
  validates :email_address, presence: true,
                        #format: {with: email_regex}, uniqueness: {message: 'there can only be one you.'}                                    
end

_form.html.erb

<%= form_for(@signup, as: :post, url: pages_path) 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 %>

解决方案

Your params are being passed in as :post and your create statement is looking for params[:signup]. You either need to change your controller to look for params[:post] or change your form to submit to params[:signup].

<%= form_for(@signup, as: :signup, url: pages_path) do |f| %>

You'll also want to add a check for whether or not the checkboxes are checked.
Presuming your parameters are:

"signup"=>{"email_address"=>"email@examp.le", "send_once"=>"0", "send_any_time"=>"1"}

You'll want to do:

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" then
      respond_to do |format| 
        if @signup.save
          format.html
        else
          format.html {render action: :index}
        end
      end
    else
      # what do you want to do if they didn't sign up?  redirect somewhere?
    end
  end
end

这篇关于复选框选择和表单对象不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆