我正在尝试在Ruby on Rails上创建一个“待办事项”应用程序,该应用程序使用简单的“名称”表单来生成待办事项列表。
我成功地创建了表单以及主索引页面,但我无法连接这两个页面。这意味着我可以输入填写表单,但是当我提交表单时,它只会将我带回索引页面,而不会列出表单项。我很难过!
以下是我的代码视图:
欢迎控制人:
class WelcomeController < ApplicationController
def index
if user_signed_in?
redirect_to :controller => "todoodoo" , :action => "index"
end
end
def home
@todos = Todoodoo.where(done: false)
@completes = Todoodoo.where(done: true)
end
end
todo controller(todoodoo):
class TodoodooController < ApplicationController
def index
@todos = Todoodoo.where(done: false)
@completes = Todoodoo.where(done: true)
end
def new
@todo = Todoodoo.new
end
def todo_params
params.require(:todoodoo).permit(:name, :done)
end
def create
@todo = Todoodoo.new(todo_params)
if @todo.save
redirect_to todoodoo_index_path, :notice => "Your To-Do item was created!"
else
render "new"
end
end
def update
@todo = Todoodoo.find(todo_params[:id])
if @todo.update_attribute(:done, true)
redirect_to todoodoo_index_path, :notice => "Your To-Do item was marked as done!"
else
redirect_to todoodoo_index_path, :notice => "Your To-Do item was unable to be marked as done!"
end
end
def destroy
@todo = Todoodoo.find(todo_params[:id])
@todo.destroy
redirect_to todoodoo_index_path, :notice => "Your To-Do item was deleted!"
end
end
这是我的todo控制器的视图文件(todoodooo):
<h1> To-Do </h1>
<h2> To Do </h2>
<% @todos.each do |t| %>
<p>
<strong><%= t.name %></strong>
<small><%= link_to "Mark as Done", todoodoo_path(t), :method => :put %></small>
</p>
<% end %>
<h2> Completed </h2>
<% @completes.each do |t| %>
<p>
<%= t.name %>
<small><%= link_to "Remove", t, :confirm => "Is it okay to remove this from the list?", :method => :delete %></small>
</p>
<% end %>
这是我的表单视图文件:
<h1> Add new item to your To-Do list </h1>
<%= form_for @todo, :url => todoodoo_index_path(@todo) do |f| %>
<%= f.label :Name %>: <%= f.text_field :name %>
<%= f.hidden_field :done, :value => false %>
<%= f.submit "Add to To-Do list", class: "btn btn-primary" %>
<% end %>
我的routes.rb文件:
Rails.application.routes.draw do
get 'welcome/home'
get 'todoodoo/index'
match "todoodoo/index" => "todoodoo#index", :via => :post
get 'people/new'
devise_for :users
root 'welcome#home'
resources :todoodoo do
member do
put :complete
end
end
我是Rails开发的新手,所以如果我错过了一些内容或者这没有意义,我会道歉。我尽力包含每个依赖文件。感谢您提供的任何输入。谢谢! (另请原谅我格式化,请知道我的代码格式没有任何错误)
答案 0 :(得分:0)
通过删除:url尝试简化表单。现在,您的表单与执行新操作和待办事项列表的索引操作之间存在混淆。
<h1> Add new item to your To-Do list </h1>
<%= form_for @todo do |f| %>
<%= f.label :Name %>: <%= f.text_field :name %>
<%= f.hidden_field :done, :value => false %>
<%= f.submit "Add to To-Do list", class: "btn btn-primary" %>
<% end %>
答案 1 :(得分:0)
既然你是新手,那么你需要做什么
顺便说一下,我高度推荐upgrading to Rails 4。如果您正在学习系统,那么使用遗留代码就没有意义了!
-
#Gemfile
gem 'rails', '4.1.5'
#config/routes.rb
root to: "todoodoos#index"
resources :todoodoo
#app/controllers/todoodoo_controller.rb
Class TodoodooController < ApplicationController
def index
@todoodoos = Tododoo.all
end
def new
@todoodoo = Todoodoo.new
end
def create
@todoodoo = Todoodoo.new(todo_params)
@todoodoo.save
end
private
def todo_params
params.require(:todoodoo).permit(:name)
end
end
这将使您能够添加以下视图:
#app/views/todoodoo/index.html.erb
<% @todoodoo.all.each do |todo| %>
<%= link_to todo.name, todo %> #-> goes to show action
<% end %>
#app/views/todoodoo/new.html.erb
<%= form_for @todoodoo do |f| %> #-> goes to create action
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
#app/views/todoodoo/create.html.erb
Succesfully created!!
我可以解释如何如果您尝试它&amp;发现它确实有用
您当前的设置非常矛盾,我觉得最好能为您提供所需的代码!