Accessing model associations in Rails 4 through a link -
i want connect user , event models, created participation model. (one user can attend many events, , 1 event can have many users participating.)
user.rb
class user < activerecord::base # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :statuses has_many :participations has_many :events, through: :participation validates_presence_of :first_name, :last_name def to_param profile_name end def full_name first_name + " " + last_name end end event.rb
class event < activerecord::base has_many :participations has_many :users, through: :participations end participation.rb
class participation < activerecord::base belongs_to :user belongs_to :event end i'm not sure how link in events index; far, have default link (link_name) in events/index.html.erb allows page load redirects individual event page when signs up:
<%= link_to link_name, event %>
i tried change part this:
<%= link_to new, new_participation_path(event) %>
which throws me error:
nameerror in events#index showing /home/ubuntu/workspace/app/views/events/index.html.erb line #10 raised: uninitialized constant actionview::compiledtemplates::new extracted source (around line #10): 7 <%= event.event_date_time.strftime("%a %d @ %i:%m %p") %></p> 8 <div class="meta"> 9 <% link_name = "sign up!" %> 10 <%= link_to new, new_participation_path(event) %> 11 <span class="admin">| <%= link_to "edit", edit_event_path(event) %> | 12 <% message = "are sure want delete event?" %> 13 <%= link_to "delete", event, method: :delete, data: { confirm: message} %> this create_participations migration:
class createparticipations < activerecord::migration
create_table :participations |t| t.belongs_to :user, index: true t.belongs_to :event, index: true t.datetime :participation_date t.timestamps null: false end end i added new definition participation controller:
def new @participation = participation.new end and here participation related routes (for reason there 2 sets):
participation /participation(.:format) participation#index new_participation /new_participation(.:format) participation#new show_participation /show_participation(.:format) participation#show participate /participate(.:format) participation#create update_participation /update_participation(.:format) participation#update delete_participation /delete_participation(.:format) participation#destroy participation_index /participation/index(.:format) participation#index participation_show /participation/show(.:format) participation#show participation_create /participation/create(.:format) participation#create participation_edit /participation/edit(.:format) participation#edit participation_update /participation/update(.:format) participation#update participation_new /participation/new(.:format) participation#new participation_destroy /participation/destroy(.:format) participation#destroy
i'm not quite sure i've gone wrong here.
here entire github repo project:
Comments
Post a Comment