Rails form_for routing syntax for custom controller

Tried forums, documentation and blog suggestions. They agree on the syntax, which for me does not correctly store and route through the desired controller.

The big picture. I have two applications for working with software. To implement common functions, I created Rails Shared :: Widgets. This MVC works fine. No problem viewing, updating, creating or deleting using standard shared / widgets, etc.

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

This circuit almost works. When I head to product1 / widgets, it sets the layout for product1 and calls the index method of common / widgets. The result is a display of shared / widgets / index.html.erb with the layout product1. Similarly, when I head to product2 / widgets, this sets up the product2 layout and calls the generic / widget index method. The result is a display of shared / widgets / index.html.erb with the product2 layout. Perfect.

But now we go to form_for. Since he implements rail magic, he really wants to go directly to the Shared :: Widgets controller. But that is not what I want. I want him to head to the appropriate product controller to set the layout. The rails generated by form_for were something like this:

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

I tried:

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

but this duplicates the 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 empty):

 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, this is due to transcription. The actual code that I used passed the interpreter. Thanks.

+6
source share
1 answer

try it

 form_for(url: { controller: "posts", action: "create" }, html => { :class => "form" }, method: "post") do |f| 
+2
source

Source: https://habr.com/ru/post/943538/


All Articles