Rails中的简单表单无法发布数据 [英] Simple form in rails can't post data

查看:106
本文介绍了Rails中的简单表单无法发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在rails中使用简单的形式.提交数据后.该页面始终返回到application_form#new,并且数据无法保存在db中.为什么?

日志

Started POST "/application_forms" for 127.0.0.1 at 2013-05-25 19:23:28 +0800
Processing by ApplicationFormsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"yypWFcsXbdSVtp3WXUZKvstzHWsV0OOpLoGSxO1ldJ0=", "application_form"=>{"student_name"=>"somebody", ...... ,"application_type"=>"b"}, "commit"=>"Submit"}
  [1m[35mUser Load (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
  [1m[35m (0.0ms)[0m  rollback transaction
  Rendered application_forms/_form.html.erb (24.0ms)
  Rendered application_forms/new.html.erb within layouts/application (25.0ms)
Completed 200 OK in 61ms (Views: 52.0ms | ActiveRecord: 0.0ms)

查看

<%= simple_form_for @application_form, :html => {:class => 'form-horizontal' } do |f| %>

  <legend>Basic Info</legend>
  <%= f.input :student_name %>
  <%= f.button :submit, 'Submit', :class => 'btn-large btn-primary'%>

<% end %>


控制器

 def create

    @application_form = ApplicationForm.new(params[:application_form])

    respond_to do |format|
      if @application_form.save
        format.html { redirect_to @application_form, :notice => 'Submit ok.' }
      else
        format.html { render :action => "new" }
      end
    end
  end

解决方案

很显然,@application_form.save失败了.如果我不得不猜,我猜您已经对模型进行了验证,但失败了.当我编写了一个好的模型(包括我想要的所有验证)时,偶尔会发生这种情况,但是后来我才开始整理一个表单,而忘记了包括所有需要存在的字段.

一种检查方法是打开

rails console

并尝试这样的事情:

@application_form = ApplicationForm.new(:student_name => "student")
@application_form.save

这几乎可以完全完成表单的工作,并且应该告诉您在尝试保存时出了什么问题.

我的猜测是,您还忘记了在布局文件中包含Flash消息.通常,当save失败时,您会在闪存中看到某种错误消息.由于您似乎正在使用Twitter Bootstrap(仅从表单视图中的form-horizontal类进行猜测),因此假设您使用的是 twitter-bootstrap-rails 宝石,您只需将<%= bootstrap_flash %>添加到视图或模板中即可.

I use simple form in rails. After submit data. the page always return to application_form#new, and data can't be saved in db. Why?

log

Started POST "/application_forms" for 127.0.0.1 at 2013-05-25 19:23:28 +0800
Processing by ApplicationFormsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"yypWFcsXbdSVtp3WXUZKvstzHWsV0OOpLoGSxO1ldJ0=", "application_form"=>{"student_name"=>"somebody", ...... ,"application_type"=>"b"}, "commit"=>"Submit"}
  [1m[35mUser Load (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
  [1m[35m (0.0ms)[0m  rollback transaction
  Rendered application_forms/_form.html.erb (24.0ms)
  Rendered application_forms/new.html.erb within layouts/application (25.0ms)
Completed 200 OK in 61ms (Views: 52.0ms | ActiveRecord: 0.0ms)

View

<%= simple_form_for @application_form, :html => {:class => 'form-horizontal' } do |f| %>

  <legend>Basic Info</legend>
  <%= f.input :student_name %>
  <%= f.button :submit, 'Submit', :class => 'btn-large btn-primary'%>

<% end %>


Controller

 def create

    @application_form = ApplicationForm.new(params[:application_form])

    respond_to do |format|
      if @application_form.save
        format.html { redirect_to @application_form, :notice => 'Submit ok.' }
      else
        format.html { render :action => "new" }
      end
    end
  end

解决方案

Well, clearly @application_form.save is failing. If I had to guess, I'd guess you've got a validation on your model that's failing. This happens to me occasionally when I've written a good model, including all the validations I will want, but then I'm just starting to put together a form and I forget to include all the fields that need to be there.

One way to check this is to open up

rails console

and try something like this:

@application_form = ApplicationForm.new(:student_name => "student")
@application_form.save

This will do pretty much exactly what your form is doing, and should tell you what the problem is when you try to save.

My guess is that you've also forgotten to include flash messages in your layout file. Usually you'll see some sort of error message in your flash when save fails. Since it seems like you're using Twitter Bootstrap (just guessing from the form-horizontal class on your form view), assuming your using the twitter-bootstrap-rails gem, you can just add <%= bootstrap_flash %> to either your view or your template.

这篇关于Rails中的简单表单无法发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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