2014-01-13 83 views
0

我找到了一個很好的解決方案,以煤礦hereRails的withdraw_event_path「未定義本地方法」

我已經內置到我自己的項目的問題,但我似乎無法調用退出事件。

show.html.erb

<p><strong>Attendees: </strong> 
<ul> 
    <% for attendee in @event.users %> 
     <% if attendee.id == current_user.id %> 
      <li><strong><%= attendee.name %></strong> 
       <%= link_to 'Withdraw From Event', withdraw_event_path(@event.id), :method => :post, :class => 'btn btn-danger' %> 
      </li> 
     <% else %> 
      <li><%= attendee.username %></li> 
     <% end %> 
    <% end %> 
</ul> 
</p> 

<p> 
    <%= link_to 'Attend Event', attend_event_path(@event.id), :method => :post %> 
</p> 

event_controller.rb

def attend 
    @event = Event.find(params[:id]) 
    current_user.events << @event 
    redirect_to @event, notice: 'You are now attending this event' 
end 

def withdraw 
    event = Event.find(params[:id]) 
    attendee = Attendee.find_by_user_id_and_event_id(current_user.id, event.id) 

    if attendee.blank? 
    redirect_to event 
    end 
    attendee.delete 
    redirect_to event, notice: 'You are no longer attending this event.' 
end 

當我打電話事件 - >顯示我得到這個錯誤

undefined local variable or method `withdraw_event_path' for #<#<Class:0x007fd5d575a910>:0x007fd5d22a72e0> 

任何想法,爲什麼路徑ISN沒有工作?我是新來的軌

感謝

回答

1

你需要添加withdraw到你的路由,將創建方法withdraw_event_path

resources :events do 
    member do 
    post 'withdraw' 
    end 
end