Rails 4: generic route

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

Paolo,

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

Cheers

Cant you do it already?

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!

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

And just a snippet from Rails Routing from the Outside In — Ruby on Rails Guides

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.

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

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?

  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…

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/”) %>

``

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

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.

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 …"

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,

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

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.