Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Question about Routes


  • Please log in to reply
5 replies to this topic

#1 g33kidd

g33kidd

    Passenger

  • Members
  • 4 posts

Posted 08 May 2014 - 04:59 PM

Hello All,

 

I'm trying to figure out this strange routing issue I want to do, but I'm not quite sure how to go about it.

 

What I have are users that own articles, which belong to the users.

 

I have tried this before, but I wasn't able to get the controller to fill both params in the URL. The Id for the article in the to_param method is just a parameterized string from the title of the article. But I have removed that to try and get this working for now.

scope ':username' do
  get ':id', to: 'articles#show', as: 'article'
end

So, in the end I would want the URL structure for an article to be like this:

example.com/username/article-title-param

or

example.com/username/mm/dd/yy/article-title-param

I hope someone has an idea, thanks! :)



#2 Ohm

Ohm

    Driver

  • Moderators
  • 469 posts
  • LocationCopenhagen

Posted 08 May 2014 - 10:14 PM

You could just go with

scope ':username' do
  get ':m/:d/:y/:id', to: 'articles#show', as: 'article'
end

and then parse the params[:m], params[:d], and params[:y] in the #show method and call a 404 if not found.


  • g33kidd likes this

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#3 g33kidd

g33kidd

    Passenger

  • Members
  • 4 posts

Posted 09 May 2014 - 12:02 AM

You could just go with

scope ':username' do
  get ':m/:d/:y/:id', to: 'articles#show', as: 'article'
end

and then parse the params[:m], params[:d], and params[:y] in the #show method and call a 404 if not found.

 

Thanks! I've done this, but now I am having problems with getting the params to work correctly.

 

I call that route with article_path(@article) in my views, but it will only populate the :username param. I can't figure out how I would construct this location properly in my views.

It seems very strange... never have done something like this before, nor can I find anything like it.



#4 Ohm

Ohm

    Driver

  • Moderators
  • 469 posts
  • LocationCopenhagen

Posted 09 May 2014 - 06:52 AM

Your article_path now takes 4 arguments. The month, the day, the year and the id (or object), so you'd have to do something like this to get a proper url from it

article_path(@article.created_at.month, @article.created_at.day, @article.created_at.year, @article)

It sucks, I know, Someone™ should create a gem for this.


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#5 g33kidd

g33kidd

    Passenger

  • Members
  • 4 posts

Posted 18 May 2014 - 07:09 AM

Your article_path now takes 4 arguments. The month, the day, the year and the id (or object), so you'd have to do something like this to get a proper url from it

article_path(@article.created_at.month, @article.created_at.day, @article.created_at.year, @article)

It sucks, I know, Someone™ should create a gem for this.

Ugh... That is extremely ugly... :D

 

That would be a great idea to make a Gem for that, though I'm not the greatest developer to do that quite yet. I will try and see where I get.

It's worth a shot.

 

Thanks for the help! :)



#6 Ohm

Ohm

    Driver

  • Moderators
  • 469 posts
  • LocationCopenhagen

Posted 18 May 2014 - 09:01 AM

You could probably have something like this in your application_helper.rb-file

def dated_path(path, object)
  date = object.created_at
  path(date.month, date.day, date.year, object)
end

which you could use like so

<%= dated_path(article_path, @article) %>

(None of it is tested)


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users