【问题标题】:Rails withdraw_event_path 'undefined local method'Railswithdraw_event_path'未定义的本地方法'
【发布时间】:2014-01-13 02:13:32
【问题描述】:

我找到了解决我的问题的好方法here

我已将它构建到我自己的项目中,但我似乎无法调用withdraw 事件。

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

当我调用 event->show 我得到这个错误

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

任何想法为什么路径不起作用?我是 Rails 新手

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 path


    【解决方案1】:

    您需要将withdraw 添加到您的路线中,这将创建方法withdraw_event_path

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2022-12-21
      相关资源
      最近更新 更多