如何在ROR中解决模板错误

时间:2014-12-18 09:47:28

标签: ruby-on-rails ruby ruby-on-rails-3

请帮我解决以下错误。当我点击新按钮时,没有任何事情发生。没有页面重定向。请给我一些解决此错误的想法。

错误:

ActionView::Template::Error (undefined method `input' for #<ActionView::Helpers:
:FormBuilder:0x46ed500>):
    1: <%= form_for @task, :url => {:action => 'create'},remote: true do |f|   %
>
    2:   <%= f.input  :description  %>
    3:   <%= f.input  :deadline     %>
    4:   <%= f.button :submit      %>
    5: <% end %>
  app/views/tasks/_form.html.erb:2:in `block in _app_views_tasks__form_html_erb_
_264285017_35597352'
  app/views/tasks/_form.html.erb:1:in `_app_views_tasks__form_html_erb__26428501
7_35597352'
  app/views/tasks/new.js.erb:1:in `_app_views_tasks_new_js_erb__302618978_315636
60'


  Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.1.7/lib/action_dispa
tch/middleware/templates/rescues/_trace.text.erb (15.6ms)
  Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.1.7/lib/action_dispa
tch/middleware/templates/rescues/_request_and_response.text.erb (0.0ms)
  Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.1.7/lib/action_dispa
tch/middleware/templates/rescues/template_error.text.erb (15.6ms)

我的代码摘要如下:

视图/任务/ index.html.erb

<div class="row">
  <div class="col-md-5 col-md-offset-1">
    <h2>Tasks</h2>
  </div>

  <div class="col-md-2 col-md-offset-4">
    <%= link_to tasks_new_path, remote: true do %>
      <button class="btn btn-default">New</button>
    <% end %>
  </div>
</div>

<div class="row">
  <div class="col-md-6 col-md-offset-2" id="task-form" style="display:none;"></div>
</div>

<div class="row">
  <div class="col-md-7 col-md-offset-1" id="tasks"><%= render @tasks %></div>
</div>

视图/任务/ new.html.erb

<h1>Its a new page</h1>
<%= render 'form' %>

视图/任务/ _form.html.erb

<%= form_for @task, :url => {:action => 'create'},remote: true do |f|   %>
  <%= f.input  :description  %>
  <%= f.input  :deadline     %>
  <%= f.button :submit      %>
<% end %>

视图/任务/ new.js.erb

$('#task-form').html("<%= j (render 'form') %>");
$('#task-form').slideDown(350);

控制器/ tasks_controller.rb

class TasksController < ApplicationController
before_action:all_tasks,only: [:index,:create]
respond_to :html, :js
def new
@task = Task.new
end
def create
@task=Task.create(tasks_params)
end
private
def all_tasks
 @tasks = Task.all
end
def tasks_params
 params.require(:task).permit(:description, :deadline)
end
end

配置/ routes.rb中

Rails.application.routes.draw do
  root "tasks#index"
  get "tasks/new" => "tasks#new" 
  post "tasks/create" => "tasks#create"
end

提前致谢。

2 个答案:

答案 0 :(得分:1)

您尝试使用来自input gem的帮助simple_form,但您似乎没有使用simple_form。我认为这些字段应该是“常见的”字样。文本字段,所以你应该这样做:

<%= form_for @task, remote: true do |f|   %>
  <%= f.text_field  :description  %>
  <%= f.text_field  :deadline     %>
  <%= f.button :submit      %>
<% end %>

答案 1 :(得分:0)

不应该

<%= form_for @task, :url => {:action => 'create'},remote: true do |f|   %>
  <%= f.text_area  :description  %>
  <%= f.text_field  :deadline     %>
  <%= f.submit   %>
<% end %>

表格形式通常如何运作。

编辑:修正了拼写错误,自动更正删除了_ ..