ActionController :: UrlGenerationError in Theme # new

0

I have this error when trying to pass parameters by link_to to another controller:

Models

class Theme < ApplicationRecord
  belongs_to :category
end

class Category < ApplicationRecord
  has_many :themes
end

Controller

def new
  @[email protected]
  @[email protected]
end

HTML

Error

Routes

Routes.rb

    
asked by Gerson 14.10.2017 в 20:53
source

1 answer

0

item.id returns value nil (i.e. has not been assigned id yet), therefore you get the error:

  

missing required keys: [: theme_id]

To solve it, verify that the object item is saved before; most likely you have generated the @category object and assigned the themes without saving the main object (ie with @category.new ) and, therefore, the id has not yet been generated so it has value nil .

Correcting my previous answer, the helper new_category_theme_comment_path if can receive parameters such as hash and as objects of ActiveRecord (in addition to numbers), therefore any of the following options is valid:

<!-- con hash -->
<%= link_to “show”, new_category_theme_comment_path(item.category_id, theme_id: item.id) %>

<!-- con número -->
<%= link_to “show”, new_category_theme_comment_path(item.category_id, item.id) %>

<!-- con objeto AR -->
<%= link_to “show”, new_category_theme_comment_path(item.category_id, item) %>
    
answered by 14.10.2017 / 22:20
source