我收到rails抱怨路线的错误。
这是链接
link_to 'Add New Rate', new_owner_property_rate_path
错误
No route matches {:action=>"new", :controller=>"owner/rates"}
路径看起来很好,好像我在路线上耙路线,
new_owner_property_rate GET /owner/properties/:property_id/rates/new(.:format) {:action=>"new", :controller=>"owner/rates"}
这是我的路径文件,它们位于名为owner的文件夹中,因此名称空间为
namespace :owner do
resources :properties do
resources :rates
end
root :to => "home#index"
end
费率控制器
class Owner::RatesController < Owner::BaseController
def index
@rates = Rate.all
end
def new
@property = Property.find(params[:property_id])
@rate = Rate.new
end
end
我可以手动输入路径吗?
任何想法
答案 0 :(得分:1)
听起来你可能没有正确重新加载路线?尝试重新启动Rails应用服务器。我已经看到路由无法识别,即使正确加载了命名路由并且路由已定义。
测试命名路由的一个很好的技巧是打开一个Rails控制台(./script/console)并在“app”快捷方式上引用,如下所示:
$> ./script/console
Loading development environment (Rails 2.3.5)
>> app.new_owner_property_rate_path
NoMethodError: undefined method `new_owner_property_rate_path' for #<ActionController::Integration::Session:0x109bd1a78>
from vendor/rails/actionpack/lib/action_controller/test_process.rb:511:in `method_missing'
from (irb):1
>>
这是我知道验证新路由并测试样本表达式或命名路由的最简单方法。这对于审查具有奇怪错误的参数化路线特别有用。
编辑:注意,Rails控制台中有一个名为“app”的对象,它是对Rails应用程序的引用,允许您测试命名路由和应用程序的许多其他方面。
答案 1 :(得分:0)
我找到了。我不得不在路径中添加对象,即
new_owner_property_rate_path(@property)