Rails form_for没有做任何事情

时间:2014-05-17 19:25:02

标签: ruby-on-rails forms

我遇到Ruby 2.0和rails 4.0的问题。

我有一个form_for创建一个新的“项目”条目,其中包含标题,描述和状态。 形式本身曾经像魅力一样工作,直到我做了一些事情,我不知道是什么。问题是,表单渲染完美,但在向其添加VALID值并按下提交按钮后,它什么都不做。如果我在输入值和提交之前刷新表单,则网址会更改为:

  

项目/新UTF8 =✓&安培; authenticity_token = Xh6ZGV2vvth1RzhBk3g5zonms%2Fdq3yzaJtzxDU%2FFBiM%3D&安培;项目%5Btitle%5D =项目+标题&安培;项目%5Bdescription%5D =描述&安培;项目%5Bstatus%5D =正在进行&安培;提交=提交

奇怪的是,如果我在同一个文件中第一个之后添加另一个完全相同的表单,那么第二个表单就可以完美运行。

我的项目new.html.erb

<h1>Create a new project</h1>
<div class="row">
  <div class="col-md-5">
    <%= form_for @project do |f| %>
        <%= f.label :title %>
      <%= f.text_field :title %>
      <%= f.label :description %>
      <%= f.text_field :description %>
      <%= f.label :status %>
      <%= f.select :status, [['Ongoing', 'Ongoing'],['Suspended','Suspended'],['Finished','Finished']] %>
      <%= f.submit "Submit" %>
    <% end %>
  </div>
</div>

projects_controller.erb

  def new
      @project = Project.new
  end

  def create
    @project = Project.new(project_params)
    if @project.save
      flash[:success] = "Project successfully created!"
      redirect_to root_path
    else
      render 'new'
    end
  end

  private

  def project_params
    params.require(:project).permit(:title, :description, :status)
  end

正如我所说的那样,之前曾经没有问题。 此外,我试图禁用JS,但它仍然无法正常工作。 如果您对问题有任何想法,请告诉我。谢谢。

编辑:

从new.html.erb生成的HTML

<div class="container">

      <h1>Create a new project</h1>
<div class="row">
  <div class="col-md-5">
    <form accept-charset="UTF-8" action="/projects" class="new_project" id="new_project" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="Xh6ZGV2vvth1RzhBk3g5zonms/dq3yzaJtzxDU/FBiM=" /></div>
      <label for="project_title">Title</label>
      <input id="project_title" name="project[title]" type="text" />
      <label for="project_description">Description</label>
      <input id="project_description" name="project[description]" type="text" />
      <label for="project_status">Status</label>
      <select id="project_status" name="project[status]"><option value="Ongoing">Ongoing</option>
<option value="Suspended">Suspended</option>
<option value="Finished">Finished</option></select>
      <input name="commit" type="submit" value="Submit" />
</form>  </div>
</div>

    </div>

提交表单后添加日志:

Started GET "/projects/new?utf8=%E2%9C%93&authenticity_token=Xh6ZGV2vvth1RzhBk3g5zonms%2Fdq3yzaJtzxDU%2FFBiM%3D&project%5Btitle%5D=Project+title&project%5Bdescription%5D=description+of+the+project&project%5Bstatus%5D=Ongoing&commit=Submit" for 127.0.0.1 at 2014-05-17 20:44:39 +0100
Processing by ProjectsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xh6ZGV2vvth1RzhBk3g5zonms/dq3yzaJtzxDU/FBiM=", "project"=>{"title"=>"Project title", "description"=>"description of the project", "status"=>"Ongoing"}, "commit"=>"Submit"}
  Rendered projects/new.html.erb within layouts/application (2.3ms)
  Rendered layouts/_header.html.erb (0.2ms)
  Rendered shared/_flash_messages.html.erb (0.1ms)
Completed 200 OK in 12ms (Views: 10.9ms | ActiveRecord: 0.0ms)


Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/rails_bootstrap_forms.css?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/project.css?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/bootstrap.css?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/bootstrap.min.css?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/projects.css?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/jquery.turbolinks.js?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/bootstrap.min.js?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/project.js?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/projects.js?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-05-17 20:44:39 +0100

1 个答案:

答案 0 :(得分:0)

我认为这不是来自rails,可能是您的浏览器的扩展,例如开发人员工具,或者可能是您的Web服务器的配置

  1. 尝试使用其他浏览器访问您的表单,最好是没有或只有少量扩展程序
  2. 如果它不起作用,请使用firebug并在Net标签中看到如果您在提交后从POST重定向到GET请求
  3. 使用其他网络服务器,切换到webrick,thin,puma或其他任何内容并查看