问题描述
我有两个相关的表。 场地
和特价
。 A 地点
可以有很多的特价
。一旦用户创建了一个场地我希望让他们的场馆#指数
页面上创建一个特殊的。通过使用嵌套的资源,我已经取得了预期的网址: /场地/ 5 /特价/新
。
不过,我目前的code结果与无路由匹配{:控制器=>中特价,:格式=>零}
我猜的错误是我的 SpecialsController
和 DEF新
和 DEF创建
功能。
我想URL带我去一个表单页面,在这里我可以为特价输入新数据
<%=的link_to'添加特殊',new_venue_special_path(场地)%GT;APP1 :: Application.routes.draw做 资源:场地做
资源:特价
结束
高清新
@venue = Venue.find(PARAMS [:venue_id])
@special = @ venue.specials.build
做的respond_to |格式|
format.html#new.html.erb
format.json {渲染JSON:@special}
结束
结束
打造高清
@venue = Venue.find(PARAMS [:venue_id])
@special = @ venue.specials.build(PARAMS [:专用])
做的respond_to |格式|
如果@ special.save
format.html {redirect_to的@special,注意:特别创建成功。 }
format.json {渲染JSON:@special,状态:创建,地点:@special}
其他
format.html {渲染动作:新}
format.json {渲染JSON:@ special.errors,状态:unprocessable_entity}
结束
结束
结束
回溯
入门GET/场地/ 4 /特价/新的127.0.0.1在2011-12-06 23点36分01秒+0200
处理由SpecialsController#新的HTML
参数:{venue_id=>中4}
[1分[36mVenue负荷(在0.2ms)[0分[1mSELECT场地。* FROM场馆WHERE场地,ID= $ 1 LIMIT 1 [0分[[ID,4]]
呈现特价/ _form.html.erb(1.9ms)
呈现特价/布局内new.html.erb /应用程序(2.6ms)
在97ms完成500内部服务器错误::的ActionView ::模板错误(没有路由匹配{:控制器=>中特价,:格式=>无}):
1:<%=的form_for(@special)做| F | %GT;
2:<%@如果special.errors.any? %GT;
3:或其可格ID =ERROR_EXPLANATION>
4:其中H 2>&下;%=复数(@ special.errors.count,错误)%>禁止这个特殊的被保存:LT; / H>
应用程序/视图/专辑/ _form.html.erb:1:`_app_views_specials__form_html_erb__2784079234875518470_70162904892440
应用程序/视图/专辑/ new.html.erb:7:`_app_views_specials_new_html_erb__115378566176177893_70162906293160
应用程序/控制器/ specials_controller.rb:30:在`新'呈现/Users/andrewlynch/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb救援内/布局(为0.7ms)
redirect_to的@special
这将默认为specials_path,但你使用venue_special_path
你可能想:
redirect_to的[@venue,@special]
和形式,您将需要相同的:
<%=的form_for([@场地,@special])做| F | %GT;
基本上 - 问题是,你有一个嵌套的资源......这意味着每个你在哪里声明一个URL路径(包括类似的form_for隐含的地方)处有与@venue和@special两者代替,而不只是@special。
您可能会遇到同样的错误在其他地方产生的脚手架code ......只是做同样的事情,你应该是不错的。
I have two associated tables. Venues
and Specials
. A venue
can have many specials
. Once a user has created a venue I wish to allow them to create a special on the venues#index
page. By using nested resources I have achieved the desired URL: /venues/5/specials/new
.
However, my current code results with: No route matches {:controller=>"specials", :format=>nil}
I'm guessing the error is with my SpecialsController
and the def new
and def create
functions.I would like the URL to take me to a form page where I can enter new data for the specials
<%= link_to 'Add Special', new_venue_special_path(venue) %>
App1::Application.routes.draw do
resources :venues do
resources :specials
end
def new
@venue = Venue.find(params[:venue_id])
@special = @venue.specials.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @special }
end
end
def create
@venue = Venue.find(params[:venue_id])
@special = @venue.specials.build(params[:special])
respond_to do |format|
if @special.save
format.html { redirect_to @special, notice: 'Special was successfully created.' }
format.json { render json: @special, status: :created, location: @special }
else
format.html { render action: "new" }
format.json { render json: @special.errors, status: :unprocessable_entity }
end
end
end
Backtrace
Started GET "/venues/4/specials/new" for 127.0.0.1 at 2011-12-06 23:36:01 +0200
Processing by SpecialsController#new as HTML
Parameters: {"venue_id"=>"4"}
[1m[36mVenue Load (0.2ms)[0m [1mSELECT "venues".* FROM "venues" WHERE "venues"."id" = $1 LIMIT 1[0m [["id", "4"]]
Rendered specials/_form.html.erb (1.9ms)
Rendered specials/new.html.erb within layouts/application (2.6ms)
Completed 500 Internal Server Error in 97ms
ActionView::Template::Error (No route matches {:controller=>"specials", :format=>nil}):
1: <%= form_for(@special) do |f| %>
2: <% if @special.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@special.errors.count, "error") %> prohibited this special from being saved:</h2>
app/views/specials/_form.html.erb:1:in `_app_views_specials__form_html_erb__2784079234875518470_70162904892440'
app/views/specials/new.html.erb:7:in `_app_views_specials_new_html_erb__115378566176177893_70162906293160'
app/controllers/specials_controller.rb:30:in `new'
Rendered /Users/andrewlynch/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.7ms)
redirect_to @special
this will default to "specials_path", but you're using venue_special_path
you probably want:
redirect_to [@venue, @special]
and in the form you will need the same:
<%= form_for([@venue, @special]) do |f| %>
basically - the issue is that you have a nested resource... which means that every place where you are declaring a url path (including implicit places like form_for) has to be replaced with both the @venue and the @special, instead of just the @special.
you may come across this same "bug" elsewhere in your generated scaffold code... just do the same thing and you should be good.
这篇关于没有路由使用嵌套资源相匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!