简体   繁体   中英

Rails custom routing not keeping URL's

I'm trying to add custom routes to my application but starting by keeping it simple.

I wish to end up with a url like:

/gallery/holidays/america/california

with each of 'holiday', 'america' and 'california' being an instance of my Collection model.

For now, I have not implemented slugs to use in the URL so at the moment the URL's need to be:

/gallery/594/595/597

with each number being the id of a collection.

Now, I have added the following route to my routing file:

get '/gallery/*path' => 'Gallery#splitUrl'

and the corresponding action is currently:

  def splitUrl
    path_parts = params[:path].split('/')

    puts path_parts[1]

    # Deal with last item of the URL
    last_item = path_parts.last

    redirect_to Collection.find(last_item) if (Collection.find_by_id(last_item))
  end

I know it's basic so far but just starting out with implementing it. It is redirecting to the appropriate collection. In the code above it is taking me to collection with id of 597 but when it does so, the URL in the address bar changes to /collections/597. How can I keep it being the /gallery/594/595/597 style?

At the end of building my app the gallery urls should be like:

/gallery/holidays/america/california/day1/

Not decided whether to actually have the individual photo on a page of it's own but if I do then it needs to be at the end of the url.

I have several models for the gallery:

Collection Album Photo PhotoSize

Collections can contain other collections OR albums but NEVER both.

I don't think I am going about this in the right way but just can't see what the right way to solve this is.

I also don't wish the /collections/ URL's to work as I want people to have to go throguh the full URL to access them.

I have tried telling the splitUrl method to render the collection#show template and created the instance variables that it requires but the rendering of the template fails as partials that the template includes are not able to be located and also as some of my styling is based on the controller name and action it fails as the new controller and action are now gallery and splitUrl.

If anyone has any ideas on how to get this to all work out then i'd highly appreciate it.

使用get '/gallery/:holiday/:country/:state =>'Gallery#splitUrl'

I think that you don't need to use redirect_to , just call a method to manipulate/render the collection right at end of the splitUrl .

If you do the redirect, it will generate another URL that is send to browser, and then the browser will ask your server to serve the new URL. If your server redirect to the same URL, this will generate a endless loop.

See more at http://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to and at http://en.wikipedia.org/wiki/HTTP_302


Edit using show method:

One way is you manage the collection in show method, like:

def show
  @collection = getCollection
  ...
end

def getCollection
  # find collection by id, if url is like /collection_id
  return Collection.find(param[:id]) if param[:id]

  # find collection with last id, if request is like /id1/id2/id3
  path_parts = params[:path].split('/')
  Collection.find(path_parts.last)
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM