Rails 新手致力于将任务与列表相关联,当我在启动 rails 服务器后尝试与表单交互时遇到麻烦。
这是我得到的错误。有任何想法吗?
谢谢!
NoMethodError in TasksController#create
undefined method `find_or_create_by_name' for #<Class:0x00000102a8bad0>
Rails.root: /Users/user/rails_projects/todolist
Application Trace | Framework Trace | Full Trace
app/models/task.rb:15:in `list_name='
app/controllers/tasks_controller.rb:43:in `new'
app/controllers/tasks_controller.rb:43:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"B7g7u+v5USPRhefdFPt84xGkKjB1nVwy62IJj6SHJpc=",
"task"=>{"description"=>"Milk",
"list_name"=>"Shopping"},
"commit"=>"Create Task"}
列表控制器:
class ListsController < ApplicationController
# GET /lists
# GET /lists.json
def index
@lists = List.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @lists }
end
end
# GET /lists/1
# GET /lists/1.json
def show
@list = List.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @list }
end
end
# GET /lists/new
# GET /lists/new.json
def new
@list = List.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @list }
end
end
# GET /lists/1/edit
def edit
@list = List.find(params[:id])
end
# POST /lists
# POST /lists.json
def create
@list = List.new(params[:list])
respond_to do |format|
if @list.save
format.html { redirect_to @list, notice: 'List was successfully created.' }
format.json { render json: @list, status: :created, location: @list }
else
format.html { render action: "new" }
format.json { render json: @list.errors, status: :unprocessable_entity }
end
end
end
# PUT /lists/1
# PUT /lists/1.json
def update
@list = List.find(params[:id])
respond_to do |format|
if @list.update_attributes(params[:list])
format.html { redirect_to @list, notice: 'List was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /lists/1
# DELETE /lists/1.json
def destroy
@list = List.find(params[:id])
@list.destroy
respond_to do |format|
format.html { redirect_to lists_url }
format.json { head :no_content }
end
end
end
任务控制器:
class TasksController < ApplicationController
# GET /tasks
# GET /tasks.json
def index
@tasks = Task.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @tasks }
end
end
# GET /tasks/1
# GET /tasks/1.json
def show
@task = Task.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @task }
end
end
# GET /tasks/new
# GET /tasks/new.json
def new
@task = Task.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @task }
end
end
# GET /tasks/1/edit
def edit
@task = Task.find(params[:id])
end
# POST /tasks
# POST /tasks.json
def create
@task = Task.new(params[:task])
respond_to do |format|
if @task.save
format.html { redirect_to @task, notice: 'Task was successfully created.' }
format.json { render json: @task, status: :created, location: @task }
else
format.html { render action: "new" }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
# PUT /tasks/1
# PUT /tasks/1.json
def update
@task = Task.find(params[:id])
respond_to do |format|
if @task.update_attributes(params[:task])
format.html { redirect_to @task, notice: 'Task was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.json
def destroy
@task = Task.find(params[:id])
@task.destroy
respond_to do |format|
format.html { redirect_to tasks_url }
format.json { head :no_content }
end
end
end
任务模型:
class Task < ActiveRecord::Base
attr_accessible :description, :list_name
belongs_to :list, :foreign_key => "list_id"
def name
@list = List.find(params[:id])
end
def list_name
list.name if self.list
end
def list_name=(str)
self.list = List.find_or_create_by_name(str)
end
end
列表型号:
class List < ActiveRecord::Base
attr_accessible :title
has_many :tasks
def name
@list = List.find(params[:id])
end
end
部分任务:
<%= form_for(@task) do |f| %>
<% if @task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% @task.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :list_name %><br />
<%= f.text_field :list_name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
部分列表:
<%= form_for(@list) do |f| %>
<% if @list.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@list.errors.count, "error") %> prohibited this list from being saved:</h2>
<ul>
<% @list.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>