Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax to route Rails form_for to custom controller

Have tried forums, documentation and blog suggestions. They converge on syntax that for me does not properly save AND route through desired controller.

Big picture, I have two software applications that share functionality. To implement shared functionality, I had Rails generate Shared::Widgets. This MVC works just fine. No problem viewing, updating, creating, or deleting using standard shared/widgets & etc routing.

Then, for the product-specific functionality, I created two controllers: Product1::Widgets and Product2::Widgets. Both inherit from Shared::Widgets controller. Both are empty except for product-specific layouts.

This scheme almost works. When I route to product1/widgets, it sets layout to product1 and invokes index method of shared/widgets. The result is display of shared/widgets/index.html.erb with product1 layout. Similarly, when I route to product2/widgets, this sets product2 layout and invokes index method of shared/widgets. The result is display of shared/widgets/index.html.erb with product2 layout. Perfect.

But now we get to the form_for. Because it implements the rails magic, it really really wants to route directly to Shared::Widgets controller. But that's not what I want. I want it to route to the appropriate product controller so as to set layout. The rails generated form_for was something like this:

form_for(@shared_widget, :html => { :class => "form"}) do |f|

I tried:

form_for([:product1, @widget], :html => { :class => "form"}) do |f| 

but that duplicates namespace (product1_widget_shared_widget_path).

I tried the following three formats, all of which seem to route correctly, and save the record, but the record is empty (columns are blank):

form_for(@widget, url => "product1/widget", :html => { :class => "form"}) do |f| 

form_for(@widget, url => url_for(:controller => "widget"), :html => { :class => "form"}) do |f| 

form_for(@widget, url => url_for(:controller => "widget", :action => "create"), :html => { :class => "form"}) do |f| 

Any help? If the above code has spelling errors, it is due to transcription. The actual code I used passed the interpreter. Thank you.

like image 835
user2315251 Avatar asked Apr 24 '13 13:04

user2315251


People also ask

How do you get a Rails route?

Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

How do routes work in Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.


1 Answers

try this

form_for(url: { controller: "posts", action: "create" }, html => { :class => "form" }, method: "post") do |f| 
like image 148
ajbraus Avatar answered Oct 08 '22 06:10

ajbraus