Ruby on rails 对RubyonRails REST路由感到困惑
我真的对RubyonRails的REST路由感到困惑。尽管我已经指定成功后,它应该转到确认操作,但它转到显示操作并传递ID=confirmRuby on rails 对RubyonRails REST路由感到困惑,ruby-on-rails,Ruby On Rails,我真的对RubyonRails的REST路由感到困惑。尽管我已经指定成功后,它应该转到确认操作,但它转到显示操作并传递ID=confirm def create @article = Article.new(params[:article]) respond_to do |format| if @article.save format.html { redirect_to :action => "confirm" } else
def create
@article = Article.new(params[:article])
respond_to do |format|
if @article.save
format.html { redirect_to :action => "confirm" }
else
format.html { render :action => "new" }
end
end
end
我得到的错误如下:
ActiveRecord::RecordNot在ArticlesController#show中找到
找不到ID=confirm的文章
Rails.root:/Projects/highoncoding
应用程序跟踪|框架跟踪|完整跟踪
app/controllers/articles\u controller.rb:31:in'show'
更新1:
这是我的Route.rb文件:
resources :articles
get "articles/confirm"
您可能应该直接在routes文件中添加confirm路由
match 'articles/confirm' => 'articles#confirm'
资源
仅适用于创建/更新/销毁/等。您可能应该直接在路由文件中添加确认路由
match 'articles/confirm' => 'articles#confirm'
资源
仅适用于创建/更新/销毁/等。您需要添加路由,使其看起来像
match 'articles/confirm/', :controller => 'article', :action => 'confirm'
resources :articles
您需要在其中包含:id,否则它会认为confirm是一个id,这就是您看到错误id=confirm的原因。还要确保这是第一条路线。(至少在文章控制器的资源之前。您需要添加路由,使其看起来像
match 'articles/confirm/', :controller => 'article', :action => 'confirm'
resources :articles
# config/routes.rb
MyApp::Application.routes.draw do
resources :articles do
member do
get 'confirm'
end
end
end
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def create
@article = Article.new(params[:article])
respond_to do |format|
if @article.save
# use a named route here
format.html { redirect_to confirm_article_url(@article) }
else
format.html { render :action => "new" }
end
end
end
end
您需要在其中包含:id,否则它会认为confirm是一个id,这就是您看到错误id=confirm的原因。还要确保这是第一个路由(至少在文章控制器的资源之前)。\config/routes.rb
# config/routes.rb
MyApp::Application.routes.draw do
resources :articles do
member do
get 'confirm'
end
end
end
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def create
@article = Article.new(params[:article])
respond_to do |format|
if @article.save
# use a named route here
format.html { redirect_to confirm_article_url(@article) }
else
format.html { render :action => "new" }
end
end
end
end
MyApp::Application.routes.draw do
参考资料:文章
成员do
获得“确认”
结束
结束
结束
#app/controllers/articles\u controller.rb
类ArticlesController<应用程序控制器
def创建
@article=article.new(参数[:article])
回应待办事项|格式|
如果@article.save
#在此处使用命名路由
format.html{重定向\u以确认\u文章\u url(@article)}
其他的
format.html{render:action=>“new”}
结束
结束
结束
结束
#config/routes.rb
MyApp::Application.routes.draw do
参考资料:文章
成员do
获得“确认”
结束
结束
结束
#app/controllers/articles\u controller.rb
类ArticlesController<应用程序控制器
def创建
@article=article.new(参数[:article])
回应待办事项|格式|
如果@article.save
#在此处使用命名路由
format.html{重定向\u以确认\u文章\u url(@article)}
其他的
format.html{render:action=>“new”}
结束
结束
结束
结束
我有以下内容:参考资料:文章获取“文章/确认”,它仅适用于RESTful操作。您正在添加新功能(确认),并且必须明确定义它。我有以下内容:参考资料:文章获取“文章/确认”,它仅适用于RESTful操作。您正在添加新功能(确认),并且必须显式地定义它。即使在像你提到的那样定义了路由之后,我也会得到以下路由错误:没有与之匹配的路由{:action=>“confirm”,:controller=>“articles”}检查以确保您有“articles”而不是article,正如上面@Jeremy所做的那样。控制器需要是article,单数,而不是复数。此外,确保您有:id段,然后您可能应该在重定向中传递:id。控制器称为“articles\u controller”,而不是“article”。我知道您根本不需要当前路由的id,您可以忽略它。即使在您提到的定义路由之后,我也会得到以下路由错误:没有路由匹配{:action=>“confirm”,:controller=>“articles”}检查以确保您有“articles”而不是article,正如上面@Jeremy所做的那样。控制器需要是article,单数,而不是复数。此外,确保您有:id段,然后您可能应该在重定向中传递:id。控制器称为“articles\u controller”,而不是“article”。我知道您根本不需要当前成员的id,您可以忽略它。我更喜欢使用confirm成员的id。我更喜欢使用confirm成员的id。