在 Ruby on Rails 3.1 中创建自定义路由时遇到问题
我似乎无法设置自定义 URL。所有 RESTful 路由都工作正常,但我不知道如何简单地将 /:unique_url
添加到我在模型中创建的现有路由(一个简单的 4 个字符随机字符串)并将提供服务作为某种“永久链接”。
Routes.rb
resources :treks
match ':unique_url' => 'treks#mobile'
Controller
.
.
def mobile
@trek = trek.find(params[:id])
end
这是因为我试图在现有资源上定义自定义操作吗?我能否在与资源相同的控制器上创建自定义方法?
顺便说一下,当我将 routes.rb
更改为 match 'treks/:id/:unique_url' => 时treks#mobile
它工作正常,但我只想将 url 简单地设置为 /:unique_url
更新 看起来像 find_by_[parameter]
是要走的路...
我一直在控制台中玩,我似乎无法获得任何方法...我可以运行 例如 Trek.last.fullname
,但无法运行@trek = Trek.last
...然后调用...@trek.lastname
例如。有什么线索吗?我认为这是我的问题。
I can't seem to set up a custom URL. All the RESTful routes work fine, but I can't figure out how to simply add /:unique_url
to the existing routes, which I create in the model (a simple 4 character random string) and will serve as the "permalink" of sorts.
Routes.rb
resources :treks
match ':unique_url' => 'treks#mobile'
Controller
.
.
def mobile
@trek = trek.find(params[:id])
end
Is this because I'm trying to define a custom action on an existing resource? Can I not create custom methods on the same controller as one with a resource?
By the way, when I change routes.rb
to match 'treks/:id/:unique_url' => treks#mobile
it works fine, but I just want the url to simply be /:unique_url
Update It seems like find_by_[parameter]
is the way to go...
I've been playing in console and I can't seem to get any methods to come forward...I can run Trek.last.fullname
for example, but cannot run @trek = Trek.last
...and then call...@trek.lastname
for example. Any clues why? I think this is my issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那么 Trek 上是否有一个字段存储其唯一的 url?如果是这样,你应该这样做:
So is there a field on Trek which stores its unique url? If so you should be doing something like this:
@pruett 不,
find_by_XXX
方法是通过 Ruby 的 method_missing 调用即时生成的!因此,您可以使用在模型中定义的任何属性来代替 XXX。您甚至可以列出多个属性,例如:
检查这些页面:
http://guides.rubyonrails .org/active_record_querying.html
http://m.onkey.org/active-record-query-interface
如果你得到一个
undefined method ... for nil:NilClass
,这意味着您尝试调用该方法的对象不存在,例如为零。您可能只是错过了在该行之前放置一个 if 语句以确保该对象非零
@pruett no, the
find_by_XXX
methods are generated on-the-fly via Ruby's method_missing call! So instead of XXX you can use any of the attributes which you defined in a model.You can even go as far as listing multiple attributes, such as:
Check these pages:
http://guides.rubyonrails.org/active_record_querying.html
http://m.onkey.org/active-record-query-interface
if you get a
undefined method ... for nil:NilClass
, it means that the object you are trying to call that method on does not exist, e.g. is nil.You probably just missed to put an if-statement before that line to make sure the object is non-nil
唔。我通常会这样做:
然后在该控制器中,ID 将不适用..您需要将其更改为如下所示:(
也就是说,如果 unique_url 是要在其下搜索的列)
Hmm. I usually would do something like this:
Then in that controller the ID isn't going to be applicable.. you'd need to change it to something like this:
(that's if unique_url is the column to search under)