Rails 4: generic route

80 views
Skip to first unread message

Paolo Di Pietro

unread,
Sep 15, 2014, 12:34:46 PM9/15/14
to rubyonra...@googlegroups.com
Hi all,

I'd like to implement (Rails 4)  a very high level (generic) abstract controller, able to manage any route and then create a viewer on the fly.

I'd like to call it 'abstracts', and being able to call as

:abstract(/:subject(/:action(/:id)))

something like

abstract/user/create
or
abstract/identity/:john/edit

I'm not sure on the best way to define the correct route, and how to generate the model and the view code on the fly, after getting the definition in the controller from the DB.

Any suggestion is appreciated.

Paolo

Vivek Sampara

unread,
Sep 15, 2014, 7:08:48 PM9/15/14
to rubyonra...@googlegroups.com
Paolo, 

Take a look at Hobo Plugin. It does something very similar using DRYML ( dont repeat yourself mark up language ) 

Cheers

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1bd51146-c7db-49ab-8666-30b9679caf5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jarmo Isotalo

unread,
Sep 16, 2014, 4:37:11 AM9/16/14
to rubyonra...@googlegroups.com
Cant you do it already?

Paolo Di Pietro

unread,
Sep 18, 2014, 4:02:27 PM9/18/14
to rubyonra...@googlegroups.com
No, I still cannot do it.

I cannot use Hobo because I'm using Neo4j NoSql db, which doesn't run with Hobo.

I just would like to set up an abstract route redirecting everything to my AbstractController!

Jarmo Isotalo

unread,
Sep 18, 2014, 4:28:53 PM9/18/14
to rubyonra...@googlegroups.com
Im not sure if I get what exactly you are trying to accomplish, but:

with route

get ':part1/(:part2/(:part3))' =>'demo#demo'

And controller
class DemoController < ApplicationController
 
def demo
    render plain
: params.inspect
 
end
end
 
And thus /foo, /foo/bar and /foo/bar/baz will use DemoController#demo and :part1 :part2 and :part3 can be accessed from params hash





3.1 Bound Parameters

When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. Two of these symbols are special: :controller maps to the name of a controller in your application, and :action maps to the name of an action within that controller. For example, consider this route:

get ':controller(/:action(/:id))'

If an incoming request of /photos/show/1 is processed by this route (because it hasn't matched any previous route in the file), then the result will be to invoke the show action of the PhotosController, and to make the final parameter "1" available as params[:id]. This route will also route the incoming request of /photos to PhotosController#index, since :actionand :id are optional parameters, denoted by parentheses.

Message has been deleted

Colin Law

unread,
Sep 18, 2014, 5:01:45 PM9/18/14
to rubyonra...@googlegroups.com
On 18 September 2014 21:52, Paolo Di Pietro <paolodi...@gmail.com> wrote:
The direction seems to be right, so I created

1) the route

    get ':part1/(:part2/(:part3))' =>'factory#demo'

2) the factory controller

Then I add the following line to my .erb

<%= link_to "some text", factory/demo/user/9 ) %>

The error message says there is an unexpected ')'  Perhaps that means there is a ')' that is unexpected.

Colin
 

but it returns the following error:

Completed 500 Internal Server Error in 45ms

SyntaxError (/home/pdipietro/gsn/app/views/identity_providers/index.html.erb:33: syntax error, unexpected ')', expecting keyword_end
... text", factory/demo/user/9 ) );@output_buffer.safe_append='
...                               ^):
  app/views/identity_providers/index.html.erb:33: syntax error, unexpected ')', expecting keyword_end

Any further suggestion?

Paolo

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Paolo Di Pietro

unread,
Sep 18, 2014, 5:04:54 PM9/18/14
to rubyonra...@googlegroups.com
Done!

Then I put
<%= link_to "some text", /factory/demo/message/1389/ %>
in my .erb file

but getting the following error:

Completed 500 Internal Server Error in 56ms

SyntaxError (/home/pdipietro/gsn/app/views/identity_providers/index.html.erb:35: syntax error, unexpected ',', expecting ')'
....append=( link_to ("some text", /factory/demo/message/1389) ...
...                               ^
/home/pdipietro/gsn/app/views/identity_providers/index.html.erb:35: syntax error, unexpected ')', expecting keyword_end
..., /factory/demo/message/1389) );@output_buffer.safe_append='
...                               ^):
  app/views/identity_providers/index.html.erb:35: syntax error, unexpected ',', expecting ')'
  app/views/identity_providers/index.html.erb:35: syntax error, unexpected ')', expecting keyword_end

I tried a lot of variants with no success!

Any idea?

Jarmo Isotalo

unread,
Sep 18, 2014, 5:05:20 PM9/18/14
to rubyonra...@googlegroups.com
1) the route excepts to get 1-3 parts, separated by / and you are trying to get 4 things to match

2) the real issue, try 
<%= link_to "some text", "factory/demo/user/9" %>
So you had missing ')' and probably next thing it would complain is that variables/methods factory/demo/user are not found...

Jarmo Isotalo

unread,
Sep 18, 2014, 5:09:43 PM9/18/14
to rubyonra...@googlegroups.com
You don't want to have white space between method name and brackets so either

<%= link_to "some text", "/factory/demo/message/1389/" %>

<%= link_to("some text", "/factory/demo/message/1389/") %>

Colin Law

unread,
Sep 18, 2014, 5:15:54 PM9/18/14
to rubyonra...@googlegroups.com
On 18 September 2014 22:04, Paolo Di Pietro <paolodi...@gmail.com> wrote:
Done!

Then I put
<%= link_to "some text", /factory/demo/message/1389/ %>
in my .erb file


I think maybe before worrying about generic routes you need to get to grips with the basics of ruby and rails.  I suggest you start by working right through a good tutorial such as railstutorial.org (which is free to use online).

Colin

Paolo Di Pietro

unread,
Sep 18, 2014, 5:27:36 PM9/18/14
to rubyonra...@googlegroups.com
Colin,

your suggestion is surely correct. My point is that I'm trying to perform an over complex task dealing with a lot of new technologies to integrate.
I need a kick to bypass this stop.

Paolo Di Pietro

unread,
Sep 18, 2014, 5:32:04 PM9/18/14
to rubyonra...@googlegroups.com
I tried the following and others, but

<%= link_to "some text", factory/demo/user/9  %>
<%= link_to "some text", factories/demo/chat %>
<%= link_to "some text","/factory/demo" %>
<%= link_to ("some text","/factory/demo") %>

not clear where  'You don't want to have white space between method name and brackets so either ...."



Il giorno giovedì 18 settembre 2014 23:15:54 UTC+2, Colin Law ha scritto:

Jarmo Isotalo

unread,
Sep 18, 2014, 6:00:45 PM9/18/14
to rubyonra...@googlegroups.com
With the first two the problem might be, that rails/ruby has no idea what factory, demo and user are - it thinks that those are methods or variables

<%= link_to "test", "/foo/bar/" %>

works when I tried it

Colin Law

unread,
Sep 19, 2014, 2:33:01 AM9/19/14
to rubyonra...@googlegroups.com
On 18 September 2014 22:27, Paolo Di Pietro <paolodi...@gmail.com> wrote:
> Colin,
>
> your suggestion is surely correct. My point is that I'm trying to perform an
> over complex task dealing with a lot of new technologies to integrate.
> I need a kick to bypass this stop.

Work through the tutorial first. A couple of days spend doing that
will save you time in the long run.

Colin

>
> Il giorno giovedì 18 settembre 2014 23:15:54 UTC+2, Colin Law ha scritto:
>>
>> On 18 September 2014 22:04, Paolo Di Pietro <paolodi...@gmail.com> wrote:
>>>
>>> Done!
>>>
>>> Then I put
>>> <%= link_to "some text", /factory/demo/message/1389/ %>
>>> in my .erb file
>>>
>>
>> I think maybe before worrying about generic routes you need to get to
>> grips with the basics of ruby and rails. I suggest you start by working
>> right through a good tutorial such as railstutorial.org (which is free to
>> use online).
>>
>> Colin
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/45ce7ed7-2265-486b-8a91-522128272022%40googlegroups.com.

Colin Law

unread,
Sep 19, 2014, 2:36:45 AM9/19/14
to rubyonra...@googlegroups.com
On 18 September 2014 22:32, Paolo Di Pietro <paolodi...@gmail.com> wrote:
> I tried the following and others, but
>
> <%= link_to "some text", factory/demo/user/9 %>
> <%= link_to "some text", factories/demo/chat %>

Those two are not valid as the second parameter you have provided
should be a variable a method or a string.

> <%= link_to "some text","/factory/demo" %>

That one appears to be valid ruby, what do you see in the terminal
when you click it?

> <%= link_to ("some text","/factory/demo") %>

Basic ruby again, a space after the method is not valid, should be
link_to(.. not link_to (..

Colin.

>
> not clear where 'You don't want to have white space between method name and
> brackets so either ...."
>
>
> Il giorno giovedì 18 settembre 2014 23:15:54 UTC+2, Colin Law ha scritto:
>>
>> On 18 September 2014 22:04, Paolo Di Pietro <paolodi...@gmail.com> wrote:
>>>
>>> Done!
>>>
>>> Then I put
>>> <%= link_to "some text", /factory/demo/message/1389/ %>
>>> in my .erb file
>>>
>>
>> I think maybe before worrying about generic routes you need to get to
>> grips with the basics of ruby and rails. I suggest you start by working
>> right through a good tutorial such as railstutorial.org (which is free to
>> use online).
>>
>> Colin
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/a7383407-ee4e-4336-8cdf-8dc2f712c977%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages