1

モデル「プロジェクト」は足場によって生成されました。

config / routers.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 %>

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

chris が指摘したように、キャンセル リンクは間違ったアクションを指しており、存在しない /projects/show を探しています。

「show」はprojects/:id/showの形式で、たまたまメンバールートです。Show は、タイプ member の継承されたリソースです。それがエラーです!

于 2013-03-01T11:34:52.883 に答える
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:

私はあなたが何をしたいのか分かりました。私が見ることができるように、あなたは初心者なので、新しいアクションで新しいオブジェクトを作成し、それを新しいフォームに転送し、「キャンセル」を押したときに新しいプロジェクトに戻りたいと思っていたと推測できますが、それはレールの方法ではありません機能。アクションで @project オブジェクトを使用して、それを html に応答するべきではありません。そして@project、新しいアクションからそれを使用することは、すべての問題を引き起こしています. アクションから削除@projectし、新しいアクションでの @project のすべての使用 (表示も)

于 2013-03-01T11:21:34.697 に答える
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 %>

projectsID = 10020 を持つテーブル内の既存のレコードを編集すると、url ヘルパーproject_path(@project)が生成できます

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

htmlで。

新しく作成する場合、url ヘルパーprojects_pathは生成できます

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

htmlで。

于 2015-08-19T09:38:18.673 に答える