How to get around a reserved word

56 views
Skip to first unread message

Walter Lee Davis

unread,
Mar 5, 2014, 2:02:34 AM3/5/14
to rubyonra...@googlegroups.com
I need to use the model name 'Action' (which will not work in Rails 4.0.3) for business reasons (it's a product name). If I name the actual model something else, is there any way to (maybe with mod_rewrite) make the URL appear the way I need it to while keeping everything working in Rails?

I've tried using the controller flag in routes.rb to try to fix this, and it seems to work, but causes a lot of problems elsewhere -- basically anything where I use the automatic URLs, like

link_to 'Link text', @instance_var

needs to be rewritten to use the long-hand route helper, like this

link_to 'Link text', action_path(@instance_var)

What am I missing here?

Thanks in advance,

Walter

Dheeraj Kumar

unread,
Mar 5, 2014, 2:28:33 AM3/5/14
to rubyonra...@googlegroups.com
Namespace it? Product::Action for the model, and product_action_path(@instance_var) in the routes.



--
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/3B7C311D-8FA4-40E7-ACCB-AEC9C499F9C9%40wdstudio.com.
For more options, visit https://groups.google.com/groups/opt_out.

Frederick Cheung

unread,
Mar 5, 2014, 6:40:08 AM3/5/14
to rubyonra...@googlegroups.com


On Wednesday, March 5, 2014 7:02:34 AM UTC, Walter Lee Davis wrote:
I need to use the model name 'Action' (which will not work in Rails 4.0.3) for business reasons (it's a product name). If I name the actual model something else, is there any way to (maybe with mod_rewrite) make the URL appear the way I need it to while keeping everything working in Rails?

I've tried using the controller flag in routes.rb to try to fix this, and it seems to work, but causes a lot of problems elsewhere -- basically anything where I use the automatic URLs, like

        link_to 'Link text', @instance_var


resources :foos, :as => :action ?

Fred
 

Walter Lee Davis

unread,
Mar 5, 2014, 8:32:18 AM3/5/14
to rubyonra...@googlegroups.com
Thanks, I tried that first, and couldn't get the forms to work. What did work was this (although it's quite a hack, routes-wise):

resources :fw_actions :as => :action
resources :fw_actions

The second line was required in order to support POST and PUT and DELETE, so I may go through these and put :only => [:post, :put, :delete] on the latter, and :without on the former.

I couldn't find any way to have form_for work in the absence of that duplicate set of routes.

Walter

> Fred
>
>
> --
> 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/af8c8dd9-dde6-4d15-aca2-1de45a2666a7%40googlegroups.com.

Jim Ruther Nill

unread,
Mar 5, 2014, 8:47:22 AM3/5/14
to rubyonra...@googlegroups.com
On Wed, Mar 5, 2014 at 9:32 PM, Walter Lee Davis <wa...@wdstudio.com> wrote:

On Mar 5, 2014, at 6:40 AM, Frederick Cheung wrote:

>
>
> On Wednesday, March 5, 2014 7:02:34 AM UTC, Walter Lee Davis wrote:
> I need to use the model name 'Action' (which will not work in Rails 4.0.3) for business reasons (it's a product name). If I name the actual model something else, is there any way to (maybe with mod_rewrite) make the URL appear the way I need it to while keeping everything working in Rails?
>
> I've tried using the controller flag in routes.rb to try to fix this, and it seems to work, but causes a lot of problems elsewhere -- basically anything where I use the automatic URLs, like
>
>         link_to 'Link text', @instance_var
>
>
> resources :foos, :as => :action ?
>

Thanks, I tried that first, and couldn't get the forms to work. What did work was this (although it's quite a hack, routes-wise):

        resources :fw_actions :as => :action
        resources :fw_actions

The second line was required in order to support POST and PUT and DELETE, so I may go through these and put :only => [:post, :put, :delete] on the latter, and :without on the former.

I couldn't find any way to have form_for work in the absence of that duplicate set of routes.

You can remove the second line.  In order for form_for to work, you need to manually set
the url instead of just form_for @instance_var.  I've tested this out using the following routes

resources :pages, as: :foo

which gave the following routes

  foo_index GET    /pages(.:format)           pages#index
            POST   /pages(.:format)           pages#create
    new_foo GET    /pages/new(.:format)       pages#new
   edit_foo GET    /pages/:id/edit(.:format)  pages#edit
        foo GET    /pages/:id(.:format)       pages#show
            PATCH  /pages/:id(.:format)       pages#update
            PUT    /pages/:id(.:format)       pages#update
            DELETE /pages/:id(.:format)       pages#destroy

Try the following for your forms

# new action
form_for @instance_var, url: foo_index_path, html: { method: :post }

# edit
form_for @instance_var, url: foo_path(@instance_var), html: { method: :put }
 

Walter

> Fred
>
>
> --
> 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/af8c8dd9-dde6-4d15-aca2-1de45a2666a7%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

--
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.




--
-------------------------------------------------------------
visit my blog at http://jimlabs.heroku.com
Reply all
Reply to author
Forward
0 new messages