为什么我对控制器的删除操作不起作用?

时间:2019-06-27 20:17:19

标签: ruby-on-rails controller

我是Rails的新手。delete动作似乎找不到我的@events并显示NoMethodError。我在这里想念什么吗?创建零件的工作方式就像一个超级按钮,但是删除link_to总是会引发错误。
 我的事件控制器及其动作:-      ```

      before_action :find_occasion
      before_action :find_event, only: [:destroy]
      def create
        @event = @occasion.events.create(params[:event].permit(:name, :location, :description, :isMakeAhead))
        redirect_to occasion_path(@occasion)
      end

      def destroy
          @event.destroy    <-- This line shows error while clicking on the link
          redirect_ to occasion_path(@occasion)
      end
      private

      def find_occasion        Got occasion id from here
        @occasion = Occasion.find(params[:occasion_id])
      end

      def find_event          Got event id from here
        @event= @occasion.events.find(params[:id])
      end
    end
And my views:-

    ```
        <%= event.name %>
        <%= event.location %>
        <%= event.description %>
        <p> <%= link_to "Delete Event", [event.occasion, event], method: :delete, data: {confirm: "Are you sure?"} %></p>

    # I have tried the button_to too but it doesn't work.

这是我使用更好错误的宝石文件时显示的内容。

    Request parameters  
    {"_method"=>"delete", "authenticity_token"=>"something", "controller"=>"events", "action"=>"destroy", "occasion_id"=>"2", "id"=>"14"}
    Rack session    
    (Object too large. Modify ActionDispatch::Request::Session#inspect or adjust BetterErrors.maximum_variable_inspect_size if you need to see it.)
    Local Variables
    Instance Variables
    @_action_has_layout 
    true
    @_routes    
    nil                  #routes shows nil i don't know why is that  
    @_action_name   
    "destroy"
    @_response_body 
    nil
    @marked_for_same_origin_verification    
    false
    @_config    
    {}
    @_params    
    <ActionController::Parameters {"_method"=>"delete",  "authenticity_token"=>"something", "controller"=>"events", "action"=>"destroy", "occasion_id"=>"2", "id"=>"14"} permitted: false> 

2 个答案:

答案 0 :(得分:0)

您正在@comment方法中设置find_event,然后在@event方法中使用变量destroy@event并未在任何地方设置。如果您使用两者之一,则可能会起作用

@comment.destroy

或者只是将您的find_event方法更改为

def find_event       
  @event = @occasion.events.find(params[:id])
end

答案 1 :(得分:0)

我的模型中有一个错字可以解决我的问题。