R
R
Ruslan Galiev2014-10-30 13:48:13
Ruby on Rails
Ruslan Galiev, 2014-10-30 13:48:13

Rails form_for how to make form generate path without module?

Hello
Model:

class Core::Contractor < ActiveRecord::Base
end

Route:
scope module: :core do
    resources :contractors
  end

I did it on purpose so that the /core View is not displayed in the url
:
= form_for @contractor, :html => { :class => "form-horizontal" } do |f|
  .form-group
    = f.label t(:name), :class => 'control-label col-md-2'
    .col-sm-3
      = f.text_field :name, :class => 'text_field form-control'
  .form-group
    .col-md-offset-2.col-sm-3
      =' f.button t(:save), :class =>'btn btn-primary', :disable_with => t(:please_wait)
      = link_to t(:cancel), contractors_path, :class => 'btn btn-default'

Swears:
undefined method `core_contractors_path' for #<#<Class:0xaa56f54>:0xafc7b00>

I so understand this way the form itself generates. Of course, I can write it like this:
= form_for @contractor, url: contractors_path, :html => { :class => "form-horizontal" } do |f|

But this will only work on the create action, on update the path is different. I don't want to make two views with the form (one for new and one for edit). Is it possible to write something in the helper itself so that it generates the path as it should?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Vsk, 2014-10-30
@HerMajor

If variations with
do not help,
then I would do this:

url: (@ contractor.persisted? ? contractor_path(@ contractor) : contractors_path, method: (@ contractor.persisted? ? :put : :post)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question