在 Ruby on Rails 3.1 中创建自定义路由时遇到问题

发布于 2024-12-14 22:26:25 字数 836 浏览 1 评论 0原文

我似乎无法设置自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红颜悴 2024-12-21 22:26:25

那么 Trek 上是否有一个字段存储其唯一的 url?如果是这样,你应该这样做:

@trek = Trek.find_by_url(params[:unique_url])

So is there a field on Trek which stores its unique url? If so you should be doing something like this:

@trek = Trek.find_by_url(params[:unique_url])
以往的大感动 2024-12-21 22:26:25
trek.find_by_unique_url( params[:unique_url] ) # should do the trick

@pruett 不,find_by_XXX 方法是通过 Ruby 的 method_missing 调用即时生成的!因此,您可以使用在模型中定义的任何属性来代替 XXX。

您甚至可以列出多个属性,例如:

find_by_name_and_unique_url( the_name, the_unigue_url)

检查这些页面:

http://guides.rubyonrails .org/active_record_querying.html

http://m.onkey.org/active-record-query-interface


如果你得到一个 undefined method ... for nil:NilClass ,这意味着您尝试调用该方法的对象不存在,例如为零。

您可能只是错过了在该行之前放置一个 if 语句以确保该对象非零

trek.find_by_unique_url( params[:unique_url] ) # should do the trick

@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:

find_by_name_and_unique_url( the_name, the_unigue_url)

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

像极了他 2024-12-21 22:26:25

唔。我通常会这样做:

map.connect "/:unique_url", :controller => "treks", :action => "mobile"

然后在该控制器中,ID 将不适用..您需要将其更改为如下所示:(

def mobile
  @trek = trek.find_by_unique_url(params[:unique_url])
end

也就是说,如果 unique_url 是要在其下搜索的列)

Hmm. I usually would do something like this:

map.connect "/:unique_url", :controller => "treks", :action => "mobile"

Then in that controller the ID isn't going to be applicable.. you'd need to change it to something like this:

def mobile
  @trek = trek.find_by_unique_url(params[:unique_url])
end

(that's if unique_url is the column to search under)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文