jeudi 16 avril 2015

Polymorphic Association in rails - Controllers and Views?

Hi I'm trying to create an event depending on the eventable type. the eventable type is either group or shop.


Is there a special way to do this ?


here is what I have done so far:


Models:



class Event < ActiveRecord::Base
belongs_to :eventable, polymorphic: true

class Group < ActiveRecord::Base
has_many :events, as: :eventable

class Shop < ActiveRecord::Base
has_many :events, as: :eventable


Routes:



resources :events do
collection do
get :new_national_event
get :new_local_event
end
do


event-controller:



def index
@search = Search.new(params[:search])
@shop = find_user_shop(@search.shop_id)
@group = @shop.group
@shop_events = @shop.events
@group_events = @group.events
end

def new_national_event
@shop = find_user_shop(@search.shop_id)
@group = @shop.group
@event = @shop.events.new
end

def new_local_event
@shop = find_user_shop(@search.shop_id)
@event = @shop.events.new
end


view:



<%=link_to 'new national event' new_national_event_path%>
<%=link_to 'new local event' new_local_event_path%>


error:



undefined local variable or method `new_national_event_path' for #<#<Class:0x007f880ee00248>:0x007f880ee37f40>

Aucun commentaire:

Enregistrer un commentaire