Skip to content

Instantly share code, notes, and snippets.

@elvisgiv
Last active December 30, 2015 13:32
Show Gist options
  • Save elvisgiv/0c18c87ed319aea3543c to your computer and use it in GitHub Desktop.
Save elvisgiv/0c18c87ed319aea3543c to your computer and use it in GitHub Desktop.

Задача - сделать так, чтобы для new и edit рендерилась одна форма при условии, что у нас есть пространство имен

руты:

       scope module: 'admin', as: 'admin' do
       ...
        resources :log_types do
          collection do
            post 'search'
          end
        end
        ...
      end

которые генерят такие урлы:

     search_admin_log_types POST     /cmsadmin/log_types/search(.:format)                                 admin/log_types#search
            admin_log_types GET      /cmsadmin/log_types(.:format)                                        admin/log_types#index
                            POST     /cmsadmin/log_types(.:format)                                        admin/log_types#create
         new_admin_log_type GET      /cmsadmin/log_types/new(.:format)                                    admin/log_types#new
        edit_admin_log_type GET      /cmsadmin/log_types/:id/edit(.:format)                               admin/log_types#edit
             admin_log_type GET      /cmsadmin/log_types/:id(.:format)                                    admin/log_types#show
                            PATCH    /cmsadmin/log_types/:id(.:format)                                    admin/log_types#update
                            PUT      /cmsadmin/log_types/:id(.:format)                                    admin/log_types#update
                            DELETE   /cmsadmin/log_types/:id(.:format)                                    admin/log_types#destroy

обычный файл _form.html.haml выглядит так:

    = simple_form_for (@log_type, html: { class: 'form-horizontal' }) do |f|
      ...

он генерит ссылку log_type_path, которой у нас нет в принципе (см. выше)

чтобы вьха отправляла форму по нужному нам пути, добавляем [:admin]:

    = simple_form_for([:admin, @log_type], html: { class: 'form-horizontal' }) do |f|

теперь вьюха отправляет форму по правильному урлу

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment