Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6800745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:00:27+00:00 2026-05-26T19:00:27+00:00

I can’t seem to set up a custom URL. All the RESTful routes work

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:00:28+00:00Added an answer on May 26, 2026 at 7:00 pm
    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can't figure out how to do this in a pretty way : I have
Can anyone help me trying to find out why this doesn't work. The brushes
Can I change the field public virtual ClassOne ClassOne { get; set; } to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can anyone give me an example of how to return the following json simply
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can anyone tell me why this doesn't work? <?php $lang = $_get[lang]; if (($lang
Can any one help me in sorting this out in sed/awk/perl Input file Start
Can anyone give me some tips on adding a custom button for each row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.