模型“项目”由脚手架生成。
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
routes.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 %>
show.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%>'>
new.html.erb:
<%- model_class = Project -%>
<div class="page-header">
<h1>New project</h1>
</div>
<%= render :partial => 'form' %>
答案 0 :(得分:1)
您可能想写_form.html.rb
:
...
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
projects_path, :class => 'btn' %>
</div>
<% end %>
(将project_path(@project)
更改为projects_path
以转到索引页。)
您尝试链接到不在数据库中的对象的显示页面。错误消息想要说的是“你告诉我链接到节目页面,但我没有身份证明。”我承认这很令人困惑。
答案 1 :(得分:0)
您是否尝试重启服务器?
如果您想确定某些路线是否正在出现或您可以使用哪条路线,请在控制台中运行:
rake routes
如果它列出了到projects / show的路由,那么你可能只需要重启服务器。但如果不是,问题可能在于拼写,或者即使您在路线中更改了match
修改强>
我刚刚注意到,在项目控制器内的新操作中,你已经做出了回应
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
删除这些行。它应该看起来像:
def new
@project = Project.new
end
编辑2:
我意识到你想做什么。我可以看到你是初学者,所以我可以假设你想在新动作中创建新对象,并将其转移到新表单,并在你点击'取消'时返回到新项目,但这不是rails功能。您不应该在您的操作中使用@project对象并将其响应到html。从新行动中使用@project
会导致所有问题。
从操作中删除@project
,并在新操作中使用@project(在视图中也是如此)
答案 2 :(得分:0)
正如chris所指出的那样,你的取消链接指向一个错误的动作,它正在寻找一个不存在的/ projects / show。
“show”具有projects /:id / show的格式,恰好是成员路由。 Show是类型成员的继承资源。这是错误!
答案 3 :(得分:0)
如果你的&#34; _form.html.rb&#34;也被&#34; edit.html.erb&#34;使用,您可以重写如下:
...
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
(@project.id.nil? ? projects_path : project_path(@project)), :class => 'btn' %>
</div>
<% end %>
如果您编辑表projects
中id = 10020的现有记录,则url helper project_path(@project)
可以生成
<a href="/projects/10020" class="btn">Cancel</a>
在html中。
如果您要创建新的,则网址助手projects_path
可以生成
<a href="/projects" class="btn">Cancel</a>
在html中。