所以我是 Rails 的新手,我一直在尝试根据当前用户创建一个非常简单的待办事项列表。到目前为止,我已经使用 Devise 处理了身份验证,并使用 simple_form gem 呈现了表单。在我尝试向数据库添加新任务之前,一切正常。出于某种原因,当我提交表单时,它会将 NULL 输入到 db 列中(当然除了增加的 id 列)......这就像值在传输中丢失或一些奇怪的东西一样。无论如何,这就是我所拥有的:
控制器:
class TodoController < ApplicationController
before_filter :authenticate_user!
def index
@todo_list = Todo.where("user_id = ?", current_user.id).all
@task = Todo.new
end
def create
@todo_list = Todo.all
@task = Todo.new(params[:task])
if @task.save
flash[:notice] = "Task Added"
redirect_to todos_path
else
render :action => 'index'
end
end
def destroy
@todo_list = Todo.find(params[:id])
@task.destroy
flash[:notice] = "Task Deleted"
redirect_to todos_path
end
end
模型:
class Todo < ActiveRecord::Base
belongs_to :users
attr_accessible :user_id, :task_name, :task_description
end
看法:
<h2>To Do List</h2>
<%= render :partial => 'form' %>
<div class="span11">
<table class="table table-condensed table-striped">
<thead>
<tr>
<th>Task</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<% @todo_list.each do |todo| %>
<tr>
<td><%= todo.task_name %></td>
<td><%= todo.task_description %></td>
<td><%= link_to("Delete", todos_path, :confirm => 'Delete task?', :method => :delete, :class => 'btn btn-mini') %></td>
</tr>
<% end %>
</table>
</div>
_form.html.erb:
<%= simple_form_for @task, :url => todos_path, :method => :post do |f| %>
<%= f.error_notification %>
<%= f.input :task_name, :as => :string %>
<%= f.input :task_description, :as => :string %>
<%= f.button :submit, :class => 'btn btn-success' %>
<% end %
这可能是非常简单的事情,我似乎无法发现它。任何帮助,将不胜感激。