Pavel Murnikov February 2016

Ruby on Rails 4 view NoMethodError

Can you help me figure out what am I doing wrong. It seems like Ruby automagically tries to use an path I dodn't define.

schema.rb

    ...
      create_table "packages", force: :cascade do |t|
        t.integer  "user_id"
        t.integer  "company_id"
        t.integer  "manager_id"
        t.string   "tag"
        ...
      end
   ...

...

routes.rb

...
      resources :users do
        resources :packages 
      end
...

packages_controller.rb

...
  def new
    @package = Package.new
    @package.user_id = params[:user_id]
    @package.manager_id = params[:manager_id] || current_user.id
    @user = safe_find User, @package.user_id, "user"
    @manager = safe_find User, @package.manager_id, "manager" 
  end
...

new.html.haml

%h1 New package

= render 'form'

= link_to 'Back', user_packages_path(@user)

_form.html.haml

= form_for @package do |f|
  = render 'shared/error_messages', object: f.object 

-#  .field
-#    = f.label :tag
-#    = f.text_field :tag


-#    = render_save_back(user_path(@user)) 

Error received

NoMethodError in PackagesController#new
undefined method `packages_path' for #<#<Class:0x00000003767c00>:0x007f8b8dcd6228>

Application Trace | Framework Trace | Full Trace
app/views/packages/_form.html.haml:1:in `_app_views_packages__form_html_haml__2133351491984004824_70118672529040'
app/views/packages/new.html.haml:3:in `_app_views_packages_new_html_haml___3329420746374611085_52010540'

Answers


Diego Steiner February 2016

= form_for @package do |f|

This will automagically try to turn @package (which is of class Package) into a String to use it with the Path-Helper. E.g. @package will try to call packages_path.

To resolve this, also pass the resource under that package is nested, like this:

= form_for [@user, @package] do |f|

Post Status

Asked in February 2016
Viewed 2,539 times
Voted 12
Answered 1 times

Search




Leave an answer