1

模型“项目”是由脚手架生成的。

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' %>
4

4 回答 4

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将您带到索引页面。)

您试图链接到不在数据库中的对象的显示页面。错误信息想说的是“你告诉我链接到显示页面,但我没有 id”。我承认这很令人困惑。

于 2013-03-01T11:25:58.983 回答
0

您是否尝试过重新启动服务器?

如果您想确定是否出现了某些路由或您可以使用哪些路由,请在控制台中运行:

rake routes

如果它列出了到项目/节目的路线,那么您可能只需要重新启动服务器。但如果不是,则问题可能出在拼写上,或者即使您更改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 的所有使用(也在视图中)

于 2013-03-01T11:21:34.697 回答
0

正如克里斯所指出的,您的取消链接指向错误的操作,并且它正在寻找不存在的 /projects/show 。

“show”的格式为projects/:id/show,恰好是成员路由。Show 是成员类型的继承资源。那是错误!

于 2013-03-01T11:34:52.883 回答
0

如果您的“_form.html.rb”也被“edit.html.erb”使用,您可以重写如下:

  ...
  <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 helperproject_path(@project)可以生成

<a href="/projects/10020" class="btn">Cancel</a>

在 html 中。

如果您正在创建新的,url helperprojects_path可以生成

<a href="/projects" class="btn">Cancel</a>

在 html 中。

于 2015-08-19T09:38:18.673 回答