【问题标题】:Ruby on Rails: /articles/:id not matching /articles/1Ruby on Rails:/articles/:id 不匹配 /articles/1
【发布时间】:2014-09-24 18:17:39
【问题描述】:

我是 Ruby on Rails 的新手,正在学习教程http://edgeguides.rubyonrails.org/getting_started.html

我从设置“文章”路由开始,在运行“rake 路由”时有以下路由:

'文章 GET /articles/:id (.:format) 文章#show'

所以,我看到有一个路径 /articles/:id,它应该映射到articles#show。但是,当我点击 url:/articles/1 时,我收到以下错误:

“未知动作 在 ArticlesController 中找不到操作“1””

我只是不确定这里发生了什么。显示在我的articles_controller.rb 中定义:

  class ArticlesController < ApplicationController

def new
end

def create
  @article = Article.new(params[:article])

  @article.save
  redirect_to @article
end

def show
  @article = Article.find(params[:id])
end

def index
  @articles = Article.all
end


private
def article_params
  params.require(:article).permit(:title,:text)
end

end

有人有想法吗?

更新:添加了 Routes.rb

RailsStarter::Application.routes.draw do
  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)
  get ':controller(/:action(/:id))'
  root :to => 'say#hello'

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  resources :articles

end

【问题讨论】:

  • 您能否也添加您的routes.rb 文件?
  • 您的路由文件几乎可以肯定是乱序的,例如,您的文章资源路由之前有一个通用的/:controller/:action 匹配器。

标签: ruby-on-rails


【解决方案1】:

从 routes.rb 文件中删除 get ':controller(/:action(/:id))'

您也应该删除(或更新)根路由,因为 say#hello 来自介绍课程。

【讨论】:

  • 嘿,谢谢!我以为那是教程中的内容,但我想我不小心取消了注释
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多