Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails路由错误:操作=>';新';返回错误“;控制器中没有与{:action=>;&“x201D;show”匹配的路由_Ruby On Rails_Rest_Rails Routing_Link To - Fatal编程技术网

Ruby on rails Rails路由错误:操作=>';新';返回错误“;控制器中没有与{:action=>;&“x201D;show”匹配的路由

Ruby on rails Rails路由错误:操作=>';新';返回错误“;控制器中没有与{:action=>;&“x201D;show”匹配的路由,ruby-on-rails,rest,rails-routing,link-to,Ruby On Rails,Rest,Rails Routing,Link To,“项目”模型由脚手架生成 config/routes.rb包含行资源:projects 我有一个从索引视图到新项目路径的链接 指向/new的链接有问题。url应用程序/project/new有以下错误: 没有路由匹配{:action=>“show”,:controller=>“projects”} 我不知道为什么,它曾经起作用,但现在不起作用了。有什么想法吗 class ProjectsController < ApplicationController # GET /projects

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

config/routes.rb包含行资源:projects

我有一个从索引视图到新项目路径的链接

指向/new的链接有问题。url应用程序/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
_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 %>
<%- 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%>'>
{:class=>'form horizontal'}do | f |%>
'控件标签'%>
'文本\字段'%>
'控件标签'%>
“文本区域”,行数:5%>
“btn btn主节点”%>
t(“helpers.links.cancel”),
项目路径(@project),:class=>'btn'>
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 %>

model\u class.model\u name.human.pluralize%>
(helpers.links.new),,
新项目路径,
:class=>'btn btn主“%”
任务计数
更新于
show.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 %>
<%- 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%>'>

@project.title%>
t(“helpers.links.edit”),
编辑项目路径(@project),:class=>'btn btn primary'>
t(“helpers.links.destroy”),
项目路径(@project),
:method=>:delete,
:data=>{:confirm=>t('.confirm',:default=>t(“helpers.links.confirm”,:default=>'确定吗?)},
:class=>“btn btn主btn危险”%>
任务:
(helpers.links.new),,
新的任务路径(:project\u id=>@project),
:class=>'btn btn主“%”
new.html.erb:

<%- model_class = Project -%>
<div class="page-header">
  <h1>New project</h1>
</div>
<%= render :partial => 'form' %>

新项目
'表格'%>

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

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

rake routes
若它列出了到项目/显示的路由,那个么您可能只需要重新启动服务器。但若并没有,那个么问题可能在于拼写,或者,即使您在路由中更改了
匹配

编辑

我刚刚注意到,在项目控制员的新动作中,您已经将响应

respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @project }
    end
删除这些行。它应该如下所示:

def new
    @project = Project.new

end
编辑2:

我意识到你想要做什么。我可以看出你是初学者,所以我可以假设你想在新动作中创建新对象,并将其转换为新形式,然后在点击“取消”时返回到新项目,但这不是rails的工作方式。你不应该在动作中使用@project object并对html做出响应。使用新行动的t
@project
导致了所有问题。
从操作中删除
@project
,以及在新操作中使用@project(也在视图中)

是否尝试重新启动服务器

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

rake routes
若它列出了到项目/显示的路由,那个么您可能只需要重新启动服务器。但若并没有,那个么问题可能在于拼写,或者,即使您在路由中更改了
匹配

编辑

我刚刚注意到,在项目控制员的新动作中,您已经将响应

respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @project }
    end
删除这些行。它应该如下所示:

def new
    @project = Project.new

end
编辑2:

我意识到你想要做什么。我可以看出你是初学者,所以我可以假设你想在新动作中创建新对象,并将其转换为新形式,然后在点击“取消”时返回到新项目,但这不是rails的工作方式。你不应该在动作中使用@project object并对html做出响应。使用新行动的t
@project
导致了所有问题。
从操作中删除
@project
,并在新操作中删除@project的所有用法(也在视图中)

您可能需要在
\u 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 %>
。。。
“btn btn主节点”%>
t(“helpers.links.cancel”),
项目路径:类=>'btn'>
(将
project\u路径(@project)
更改为
projects\u路径
以进入索引页。)


您正在尝试链接到数据库中不存在的对象的显示页面。错误消息要说的是“您告诉我链接到显示页面,但我没有id”。我承认这很混乱。

您可能需要在
\u 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 %>
。。。
“btn btn主节点”%>
t(“helpers.links.cancel”),
项目路径:类=>'btn'>
(将
project\u路径(@project)
更改为
projects\u路径
以进入索引页。)


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

正如chris所指出的,您的取消链接指向错误的操作,它正在查找不存在的/projects/show


“show”的格式为projects/:id/show,它恰好是成员路由。show是一个继承的member类型的资源。这就是错误!

正如chris所指出的,您的取消链接指向错误的操作,它正在查找不存在的/projects/show

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

如果您的“\u 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 %>
在html中

如果您正在创建新的url帮助程序,则可以生成url帮助程序
projects\u path

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

在html中。

如果