Module route in Rails with form_for(@object)









up vote
2
down vote

favorite












I have namespaced Controller Entities::Customers



class Entities::CustomersController < ApplicationController
...
end


and namespaced ActiveRecord model:



class Entities::Customer < Entities::User

end


in my routes.rb file i have:



 resources :customers, module: :entities


The module :entities is there because i don't want to have routes such as:



/entities/customers but only:



/customers.



The problem starts when i'm rendering my form:



<%= simple_form_for(@customer) do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :name %>
<%= f.button :submit %>
<% end %>


This throws error: undefined method `entities_customer_path' for Class..



So the error is that rails think that the correct path is with prefix entities.



Rake routes give me:



 Prefix Verb URI Pattern Controller#Action
customers GET /customers(.:format) entities/customers#index
POST /customers(.:format) entities/customers#create
new_customer GET /customers/new(.:format) entities/customers#new
edit_customer GET /customers/:id/edit(.:format) entities/customers#edit
customer GET /customers/:id(.:format) entities/customers#show
PATCH /customers/:id(.:format) entities/customers#update
PUT /customers/:id(.:format) entities/customers#update
DELETE /customers/:id(.:format) entities/customers#destroy









share|improve this question























  • provide please your routes for customer
    – Малъ Скрылевъ
    Jun 1 '16 at 6:52










  • resources :customers, module: :entities as seen in description
    – Martin Svoboda
    Jun 1 '16 at 10:44











  • I've meant output of rake routes
    – Малъ Скрылевъ
    Jun 1 '16 at 10:50










  • OK. I've added rake routes output.
    – Martin Svoboda
    Jun 1 '16 at 12:07














up vote
2
down vote

favorite












I have namespaced Controller Entities::Customers



class Entities::CustomersController < ApplicationController
...
end


and namespaced ActiveRecord model:



class Entities::Customer < Entities::User

end


in my routes.rb file i have:



 resources :customers, module: :entities


The module :entities is there because i don't want to have routes such as:



/entities/customers but only:



/customers.



The problem starts when i'm rendering my form:



<%= simple_form_for(@customer) do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :name %>
<%= f.button :submit %>
<% end %>


This throws error: undefined method `entities_customer_path' for Class..



So the error is that rails think that the correct path is with prefix entities.



Rake routes give me:



 Prefix Verb URI Pattern Controller#Action
customers GET /customers(.:format) entities/customers#index
POST /customers(.:format) entities/customers#create
new_customer GET /customers/new(.:format) entities/customers#new
edit_customer GET /customers/:id/edit(.:format) entities/customers#edit
customer GET /customers/:id(.:format) entities/customers#show
PATCH /customers/:id(.:format) entities/customers#update
PUT /customers/:id(.:format) entities/customers#update
DELETE /customers/:id(.:format) entities/customers#destroy









share|improve this question























  • provide please your routes for customer
    – Малъ Скрылевъ
    Jun 1 '16 at 6:52










  • resources :customers, module: :entities as seen in description
    – Martin Svoboda
    Jun 1 '16 at 10:44











  • I've meant output of rake routes
    – Малъ Скрылевъ
    Jun 1 '16 at 10:50










  • OK. I've added rake routes output.
    – Martin Svoboda
    Jun 1 '16 at 12:07












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have namespaced Controller Entities::Customers



class Entities::CustomersController < ApplicationController
...
end


and namespaced ActiveRecord model:



class Entities::Customer < Entities::User

end


in my routes.rb file i have:



 resources :customers, module: :entities


The module :entities is there because i don't want to have routes such as:



/entities/customers but only:



/customers.



The problem starts when i'm rendering my form:



<%= simple_form_for(@customer) do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :name %>
<%= f.button :submit %>
<% end %>


This throws error: undefined method `entities_customer_path' for Class..



So the error is that rails think that the correct path is with prefix entities.



Rake routes give me:



 Prefix Verb URI Pattern Controller#Action
customers GET /customers(.:format) entities/customers#index
POST /customers(.:format) entities/customers#create
new_customer GET /customers/new(.:format) entities/customers#new
edit_customer GET /customers/:id/edit(.:format) entities/customers#edit
customer GET /customers/:id(.:format) entities/customers#show
PATCH /customers/:id(.:format) entities/customers#update
PUT /customers/:id(.:format) entities/customers#update
DELETE /customers/:id(.:format) entities/customers#destroy









share|improve this question















I have namespaced Controller Entities::Customers



class Entities::CustomersController < ApplicationController
...
end


and namespaced ActiveRecord model:



class Entities::Customer < Entities::User

end


in my routes.rb file i have:



 resources :customers, module: :entities


The module :entities is there because i don't want to have routes such as:



/entities/customers but only:



/customers.



The problem starts when i'm rendering my form:



<%= simple_form_for(@customer) do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :name %>
<%= f.button :submit %>
<% end %>


This throws error: undefined method `entities_customer_path' for Class..



So the error is that rails think that the correct path is with prefix entities.



Rake routes give me:



 Prefix Verb URI Pattern Controller#Action
customers GET /customers(.:format) entities/customers#index
POST /customers(.:format) entities/customers#create
new_customer GET /customers/new(.:format) entities/customers#new
edit_customer GET /customers/:id/edit(.:format) entities/customers#edit
customer GET /customers/:id(.:format) entities/customers#show
PATCH /customers/:id(.:format) entities/customers#update
PUT /customers/:id(.:format) entities/customers#update
DELETE /customers/:id(.:format) entities/customers#destroy






ruby-on-rails controller routes namespaces simple-form-for






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 1 '16 at 18:12

























asked May 31 '16 at 22:19









Martin Svoboda

951111




951111











  • provide please your routes for customer
    – Малъ Скрылевъ
    Jun 1 '16 at 6:52










  • resources :customers, module: :entities as seen in description
    – Martin Svoboda
    Jun 1 '16 at 10:44











  • I've meant output of rake routes
    – Малъ Скрылевъ
    Jun 1 '16 at 10:50










  • OK. I've added rake routes output.
    – Martin Svoboda
    Jun 1 '16 at 12:07
















  • provide please your routes for customer
    – Малъ Скрылевъ
    Jun 1 '16 at 6:52










  • resources :customers, module: :entities as seen in description
    – Martin Svoboda
    Jun 1 '16 at 10:44











  • I've meant output of rake routes
    – Малъ Скрылевъ
    Jun 1 '16 at 10:50










  • OK. I've added rake routes output.
    – Martin Svoboda
    Jun 1 '16 at 12:07















provide please your routes for customer
– Малъ Скрылевъ
Jun 1 '16 at 6:52




provide please your routes for customer
– Малъ Скрылевъ
Jun 1 '16 at 6:52












resources :customers, module: :entities as seen in description
– Martin Svoboda
Jun 1 '16 at 10:44





resources :customers, module: :entities as seen in description
– Martin Svoboda
Jun 1 '16 at 10:44













I've meant output of rake routes
– Малъ Скрылевъ
Jun 1 '16 at 10:50




I've meant output of rake routes
– Малъ Скрылевъ
Jun 1 '16 at 10:50












OK. I've added rake routes output.
– Martin Svoboda
Jun 1 '16 at 12:07




OK. I've added rake routes output.
– Martin Svoboda
Jun 1 '16 at 12:07












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Ok so after some struggling I've found a solution to this problem:



The simple_form_for(@model) generate route prefixed with entities, since it doesn't know there is scoped path in routes.



So in my _form partial i had to manually tell rails which route to use depending on action_name helper method in my partial.



<%
case action_name
when 'new', 'create'
action = send("customers_path")
method = :post
when 'edit', 'update'
action = send("customer_path", @customer)
method = :put
end
%>

<%= simple_form_for(@customer, url: action, method: method) do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :name %>
<%= f.button :submit %>
<% end %>





share|improve this answer






















  • Haven't you found a global solution? something Scalable to the entire project ?
    – Albert Català
    Nov 9 at 11:01










  • I don't recall solving this in a nice way. Maybe simple_form has fixed it by now, or potentially it's Rails issue which have been fixed in Rails 5. I don't remember which part of code actually generated the incorrect route.
    – Martin Svoboda
    Nov 9 at 16:11






  • 1




    I've just had this problem with form_with, and I've added an answer, it is like a global solution as asked for
    – Albert Català
    Nov 9 at 16:13

















up vote
0
down vote













A global solution for all project can be overriding ApplicationHelper method form_with (currently in Rails 5):



in aplication_helper.rb



 def form_with(**options)
if options[:model]
class_name = options[:model].class.name.demodulize.underscore
create_route_name = class_name.pluralize

options[:scope] = class_name
options[:url] = if options[:model].new_record?
send("#create_route_name_path")
# form action = "customers_path"
else
send("#class_name_path", options[:model])
# form action = "customer/45"
end

# post for create and patch for update:
options[:method] = :patch if options[:model].persisted?

options[:model] = nil

super
end
end


So that, in case to have routes like



 scope module: 'site' do
resources :translations
end


you can code in your _form.html.erb:



<%= form_with(model: @translation, method: :patch) do |form| %>


without errors






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37556575%2fmodule-route-in-rails-with-form-forobject%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Ok so after some struggling I've found a solution to this problem:



    The simple_form_for(@model) generate route prefixed with entities, since it doesn't know there is scoped path in routes.



    So in my _form partial i had to manually tell rails which route to use depending on action_name helper method in my partial.



    <%
    case action_name
    when 'new', 'create'
    action = send("customers_path")
    method = :post
    when 'edit', 'update'
    action = send("customer_path", @customer)
    method = :put
    end
    %>

    <%= simple_form_for(@customer, url: action, method: method) do |f| %>
    <%= f.input :email %>
    <%= f.input :password %>
    <%= f.input :name %>
    <%= f.button :submit %>
    <% end %>





    share|improve this answer






















    • Haven't you found a global solution? something Scalable to the entire project ?
      – Albert Català
      Nov 9 at 11:01










    • I don't recall solving this in a nice way. Maybe simple_form has fixed it by now, or potentially it's Rails issue which have been fixed in Rails 5. I don't remember which part of code actually generated the incorrect route.
      – Martin Svoboda
      Nov 9 at 16:11






    • 1




      I've just had this problem with form_with, and I've added an answer, it is like a global solution as asked for
      – Albert Català
      Nov 9 at 16:13














    up vote
    2
    down vote



    accepted










    Ok so after some struggling I've found a solution to this problem:



    The simple_form_for(@model) generate route prefixed with entities, since it doesn't know there is scoped path in routes.



    So in my _form partial i had to manually tell rails which route to use depending on action_name helper method in my partial.



    <%
    case action_name
    when 'new', 'create'
    action = send("customers_path")
    method = :post
    when 'edit', 'update'
    action = send("customer_path", @customer)
    method = :put
    end
    %>

    <%= simple_form_for(@customer, url: action, method: method) do |f| %>
    <%= f.input :email %>
    <%= f.input :password %>
    <%= f.input :name %>
    <%= f.button :submit %>
    <% end %>





    share|improve this answer






















    • Haven't you found a global solution? something Scalable to the entire project ?
      – Albert Català
      Nov 9 at 11:01










    • I don't recall solving this in a nice way. Maybe simple_form has fixed it by now, or potentially it's Rails issue which have been fixed in Rails 5. I don't remember which part of code actually generated the incorrect route.
      – Martin Svoboda
      Nov 9 at 16:11






    • 1




      I've just had this problem with form_with, and I've added an answer, it is like a global solution as asked for
      – Albert Català
      Nov 9 at 16:13












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Ok so after some struggling I've found a solution to this problem:



    The simple_form_for(@model) generate route prefixed with entities, since it doesn't know there is scoped path in routes.



    So in my _form partial i had to manually tell rails which route to use depending on action_name helper method in my partial.



    <%
    case action_name
    when 'new', 'create'
    action = send("customers_path")
    method = :post
    when 'edit', 'update'
    action = send("customer_path", @customer)
    method = :put
    end
    %>

    <%= simple_form_for(@customer, url: action, method: method) do |f| %>
    <%= f.input :email %>
    <%= f.input :password %>
    <%= f.input :name %>
    <%= f.button :submit %>
    <% end %>





    share|improve this answer














    Ok so after some struggling I've found a solution to this problem:



    The simple_form_for(@model) generate route prefixed with entities, since it doesn't know there is scoped path in routes.



    So in my _form partial i had to manually tell rails which route to use depending on action_name helper method in my partial.



    <%
    case action_name
    when 'new', 'create'
    action = send("customers_path")
    method = :post
    when 'edit', 'update'
    action = send("customer_path", @customer)
    method = :put
    end
    %>

    <%= simple_form_for(@customer, url: action, method: method) do |f| %>
    <%= f.input :email %>
    <%= f.input :password %>
    <%= f.input :name %>
    <%= f.button :submit %>
    <% end %>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 20 '17 at 14:02

























    answered Jul 7 '16 at 16:50









    Martin Svoboda

    951111




    951111











    • Haven't you found a global solution? something Scalable to the entire project ?
      – Albert Català
      Nov 9 at 11:01










    • I don't recall solving this in a nice way. Maybe simple_form has fixed it by now, or potentially it's Rails issue which have been fixed in Rails 5. I don't remember which part of code actually generated the incorrect route.
      – Martin Svoboda
      Nov 9 at 16:11






    • 1




      I've just had this problem with form_with, and I've added an answer, it is like a global solution as asked for
      – Albert Català
      Nov 9 at 16:13
















    • Haven't you found a global solution? something Scalable to the entire project ?
      – Albert Català
      Nov 9 at 11:01










    • I don't recall solving this in a nice way. Maybe simple_form has fixed it by now, or potentially it's Rails issue which have been fixed in Rails 5. I don't remember which part of code actually generated the incorrect route.
      – Martin Svoboda
      Nov 9 at 16:11






    • 1




      I've just had this problem with form_with, and I've added an answer, it is like a global solution as asked for
      – Albert Català
      Nov 9 at 16:13















    Haven't you found a global solution? something Scalable to the entire project ?
    – Albert Català
    Nov 9 at 11:01




    Haven't you found a global solution? something Scalable to the entire project ?
    – Albert Català
    Nov 9 at 11:01












    I don't recall solving this in a nice way. Maybe simple_form has fixed it by now, or potentially it's Rails issue which have been fixed in Rails 5. I don't remember which part of code actually generated the incorrect route.
    – Martin Svoboda
    Nov 9 at 16:11




    I don't recall solving this in a nice way. Maybe simple_form has fixed it by now, or potentially it's Rails issue which have been fixed in Rails 5. I don't remember which part of code actually generated the incorrect route.
    – Martin Svoboda
    Nov 9 at 16:11




    1




    1




    I've just had this problem with form_with, and I've added an answer, it is like a global solution as asked for
    – Albert Català
    Nov 9 at 16:13




    I've just had this problem with form_with, and I've added an answer, it is like a global solution as asked for
    – Albert Català
    Nov 9 at 16:13












    up vote
    0
    down vote













    A global solution for all project can be overriding ApplicationHelper method form_with (currently in Rails 5):



    in aplication_helper.rb



     def form_with(**options)
    if options[:model]
    class_name = options[:model].class.name.demodulize.underscore
    create_route_name = class_name.pluralize

    options[:scope] = class_name
    options[:url] = if options[:model].new_record?
    send("#create_route_name_path")
    # form action = "customers_path"
    else
    send("#class_name_path", options[:model])
    # form action = "customer/45"
    end

    # post for create and patch for update:
    options[:method] = :patch if options[:model].persisted?

    options[:model] = nil

    super
    end
    end


    So that, in case to have routes like



     scope module: 'site' do
    resources :translations
    end


    you can code in your _form.html.erb:



    <%= form_with(model: @translation, method: :patch) do |form| %>


    without errors






    share|improve this answer


























      up vote
      0
      down vote













      A global solution for all project can be overriding ApplicationHelper method form_with (currently in Rails 5):



      in aplication_helper.rb



       def form_with(**options)
      if options[:model]
      class_name = options[:model].class.name.demodulize.underscore
      create_route_name = class_name.pluralize

      options[:scope] = class_name
      options[:url] = if options[:model].new_record?
      send("#create_route_name_path")
      # form action = "customers_path"
      else
      send("#class_name_path", options[:model])
      # form action = "customer/45"
      end

      # post for create and patch for update:
      options[:method] = :patch if options[:model].persisted?

      options[:model] = nil

      super
      end
      end


      So that, in case to have routes like



       scope module: 'site' do
      resources :translations
      end


      you can code in your _form.html.erb:



      <%= form_with(model: @translation, method: :patch) do |form| %>


      without errors






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        A global solution for all project can be overriding ApplicationHelper method form_with (currently in Rails 5):



        in aplication_helper.rb



         def form_with(**options)
        if options[:model]
        class_name = options[:model].class.name.demodulize.underscore
        create_route_name = class_name.pluralize

        options[:scope] = class_name
        options[:url] = if options[:model].new_record?
        send("#create_route_name_path")
        # form action = "customers_path"
        else
        send("#class_name_path", options[:model])
        # form action = "customer/45"
        end

        # post for create and patch for update:
        options[:method] = :patch if options[:model].persisted?

        options[:model] = nil

        super
        end
        end


        So that, in case to have routes like



         scope module: 'site' do
        resources :translations
        end


        you can code in your _form.html.erb:



        <%= form_with(model: @translation, method: :patch) do |form| %>


        without errors






        share|improve this answer














        A global solution for all project can be overriding ApplicationHelper method form_with (currently in Rails 5):



        in aplication_helper.rb



         def form_with(**options)
        if options[:model]
        class_name = options[:model].class.name.demodulize.underscore
        create_route_name = class_name.pluralize

        options[:scope] = class_name
        options[:url] = if options[:model].new_record?
        send("#create_route_name_path")
        # form action = "customers_path"
        else
        send("#class_name_path", options[:model])
        # form action = "customer/45"
        end

        # post for create and patch for update:
        options[:method] = :patch if options[:model].persisted?

        options[:model] = nil

        super
        end
        end


        So that, in case to have routes like



         scope module: 'site' do
        resources :translations
        end


        you can code in your _form.html.erb:



        <%= form_with(model: @translation, method: :patch) do |form| %>


        without errors







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 23:47

























        answered Nov 9 at 16:09









        Albert Català

        1,29722029




        1,29722029



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37556575%2fmodule-route-in-rails-with-form-forobject%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党