模型“项目”是由脚手架生成的。
config/routes.rb 包含行资源 :projects。
我有一个从索引视图到 new_project_path 的链接。
问题在于 /new 的链接。url application/project/new 有以下错误:
没有路线匹配 {:action=>"show", :controller=>"projects"}
以前可以用,现在不行了,不知道为什么。有任何想法吗?
class ProjectsController < ApplicationController
# GET /projects
# GET /projects.json
def index
@projects = Project.paginate(:page=>params[:page],:per_page=>15)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @projects }
end
end
# GET /projects/1
# GET /projects/1.json
def show
@project = Project.find(params[:id])
@tasks=@project.tasks.paginate(:page=>params[:page],:per_page=>15)
respond_to do |format|
format.html # show.html.erb
format.json { render json: @project }
end
end
# GET /projects/new
# GET /projects/new.json
def new
@project = Project.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end
# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(params[:project])
respond_to do |format|
if @project.save
format.html { redirect_to projects_path, notice: 'Project was successfully created.' }
format.json { render json: @project, status: :created, location: @project }
else
format.html { render action: "new" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PUT /projects/1
# PUT /projects/1.json
def update
@project = Project.find(params[:id])
respond_to do |format|
if @project.update_attributes(params[:project])
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project = Project.find(params[:id])
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url }
format.json { head :no_content }
end
end
def sort_tasks
project = Project.find(params[:id])
tasks = project.tasks
tasks.each do |task|
task.position = params['task'].index(task.id.to_s) + 1
task.save
end
render :nothing => true
end
end
路线.rb:
Taska::Application.routes.draw do
resources :projects
resources :tasks
root :to => 'projects#index'
match ':controller(/:action(/:id))(.:format)'
end
_form.html.erb:
<%= form_for @project, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :title, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :title, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :description, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :description, :class => 'text_area', rows: 5 %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
project_path(@project), :class => 'btn' %>
</div>
<% end %>
index.htm.erb:
<%- model_class = Project -%>
<div class="page-header">
<h3><%=t '.title', :default => model_class.model_name.human.pluralize %></h3>
<%= link_to t('.new', :default => t("helpers.links.new")),
new_project_path,
:class => 'btn btn-primary' %>
</div>
<table class="table table-striped">
<thead>
<tr>
<th><%= model_class.human_attribute_name(:title) %></th>
<th>Tasks count</th>
<th>Updated at</th>
</tr>
</thead>
<tbody>
<% @projects.each do |project| %>
<tr>
<td><%= link_to project.title, project_path(project) %></td>
<td><%= project.tasks.size %></td>
<td><%= project.updated_at %></td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate @projects %>
显示.html.erb:
<%- model_class = Project -%>
<div class="page-header">
<h1><%=t '.title', :default => @project.title %></h1>
<h4><%= @project.description %></h4>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_project_path(@project), :class => 'btn btn-primary' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
project_path(@project),
:method => :delete,
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-primary btn-danger' %>
<h2>Tasks:</h2>
<%= link_to t('.new', :default => t("helpers.links.new")),
new_task_path(:project_id => @project),
:class => 'btn btn-primary' %>
</div>
<table class="table table-striped">
<thead>
<tr>
<th></td>
<th><%= model_class.human_attribute_name(:type) %></th>
<th><%= model_class.human_attribute_name(:title) %></th>
<th><%= model_class.human_attribute_name(:status) %></th>
<th><%= model_class.human_attribute_name(:updated_at) %></th>
</tr>
</thead>
<tbody id="tasks_list">
<% @tasks.each do |task| %>
<tr id="task_<%= task.id %>" class="handle" >
<td><%= link_to "open task",task_path(task) %></td>
<td><%= task.type.name %></td>
<td><%= task.title %></td>
<td><%= task.status.name %></td>
<td><%= task.updated_at %></td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate @tasks %>
<input type="hidden" id="project_id" value='<%=@project.id%>'>
新的.html.erb:
<%- model_class = Project -%>
<div class="page-header">
<h1>New project</h1>
</div>
<%= render :partial => 'form' %>