TAGS :Viewed: 8 - Published at: a few seconds ago
[ undefined method `category` for nil:NilClass rails 4 ]
In views/offers/index:
<%= f.collection_select :catId_get, Category.order(:name), :id, :name,
{ include_blank: true },
{data:{
dynamic_selectable_url: dynamic_select_offers_path(':catId_get'),
dynamic_selectable_target: '#offer_menu_id'
}} %>
<%= f.collection_select :menuName_get, @offer.Category.try(:menus) || [], :id,:menu_item_name, :include_blank => true %>
In views/dynamic_select/offers/index.json.jbuilder:
json.array!(@menus) do |menu|
json.extract! menu, :menu_item_name, :id
end
In controllers/dynamic_select/offers_controller.rb:
module DynamicSelect
class OffersController < ApplicationController
respond_to :json
def index
@menus=Menu.where(:category_id=>params[:catId_get])
respond_with(@menus)
end
end
end
In javascripts/dynamic_selectable.js.coffee:
$.fn.extend
dynamicSelectable: ->
$(@).each (i, el) ->
new DynamicSelectable($(el))
class DynamicSelectable
constructor: ($select) ->
@init($select)
init: ($select) ->
@urlTemplate = $select.data('dynamicSelectableUrl')
@$targetSelect = $($select.data('dynamicSelectableTarget'))
$select.on 'change', =>
@clearTarget()
url = @constructUrl($select.val())
if url
$.getJSON url, (data) =>
$.each data, (index, el) =>
@$targetSelect.append "<option value='#{el.id}'>#{el.name}</option>"
@reinitializeTarget()
else
@reinitializeTarget()
reinitializeTarget: ->
@$targetSelect.trigger("change")
clearTarget: ->
@$targetSelect.html('<option></option>')
constructUrl: (id) ->
if id && id != ''
@urlTemplate.replace(/:.+_id/, id)
In javascripts/init.js.coffee:
window.initApp = ->
$('select[data-dynamic-selectable-url][data-dynamic-selectable-target]').dynamicSelectable()
document.addEventListener 'page:load', initApp
$ initApp
In db/migrate:
create_table :menus do |t|
t.integer 'hotel_id'
t.string 'menu_item_name'
t.integer 'price'
t.string 'item_type'
end
add_index("menus","hotel_id")#index is used to search
end
create_table :categories do |t|
t.string 'name'
t.integer 'hotel_id'
end
add_index("categories","hotel_id")
end
I am new in rails.I want to populate data on the menu_item dropdown when i select a category from its parent dropdown. Please help me, i have been stucked in this for 2 days.
Started GET "/offers/index" for 127.0.0.1 at 2014-10-29 16:25:57 +0530
Processing by OffersController#index as HTML
←[1m←[35mCategory Load (15.6ms)←[0m SELECT `categories`.*
FROM `categories` ORDER BY `categories`.`name` ASC
Rendered
offers/index.html.erb within layouts/application (15.6ms)
Completed
500 Internal Server Error in 32ms
ActionView::Template::Error
(undefined method `Category' for nil:NilClass):
316:
<div>
317: <%= f.label "Select Menu:" %>
&nbsp;
318:
319: <%= f.collection_select
:menuName_get, @offer.Category(:menus) || [], :id,:menu_item_name,
:include_blank => true %>
320:
321:
322:
</div>
app/views/offers/index.html.erb:319:in `block in
_app_views_offers_index_html_erb___717332836_61645344'
app/views/offers/index.html.erb:303:in
`_app_views_offers_index_html_erb___717332836_61645344'
Answer 1
You have not set @offer
in your controller, something like:
@offer = Offer.first
Then you can use:
@offer.categories
Not @offer.Category
note the down case and plural.