【问题标题】:Heroku Page doesn't exist for custom put action自定义放置操作不存在 Heroku 页面
【发布时间】:2014-03-15 02:27:13
【问题描述】:

我正在关注 Railscast 035 自定义 REST 操作。我的一切都在本地工作,但是当我推送到 heroku 时,我得到一个页面不存在 /tasks/2/complete Heroku App GitHub Repo 的错误

当我点击complete task 链接时,我应该为rails 应用程序的tasks#complete 操作创建一个put 请求。相反,我收到 404 错误。在我的日志中,我有以下内容

method=GET path=/task/2/complete host=custom-rest-actions.herokuapp.com
request_id=16761046-c4fc-4164-b0bb-f6ed6191d71a fwd="98.207.180.92" dyno=web.1 
connect=7ms service=19ms status=404 bytes=1829

heroku run rake routes

         Prefix Verb   URI Pattern                   Controller#Action
           root GET    /                             tasks#index
completed_tasks GET    /tasks/completed(.:format)    tasks#completed
  complete_task PUT    /tasks/:id/complete(.:format) tasks#complete
          tasks GET    /tasks(.:format)              tasks#index
                POST   /tasks(.:format)              tasks#create
       new_task GET    /tasks/new(.:format)          tasks#new
      edit_task GET    /tasks/:id/edit(.:format)     tasks#edit
           task GET    /tasks/:id(.:format)          tasks#show
                PATCH  /tasks/:id(.:format)          tasks#update
                PUT    /tasks/:id(.:format)          tasks#update
                DELETE /tasks/:id(.:format)          tasks#destroy

Index.html.erb

<table>
  <tbody>
    <% @tasks.each do |task| %>
      <tr>
        <td><%= link_to task.name, task %></td>
        <td>| <%= link_to 'Edit', edit_task_path(task) %></td>
        <td><%= link_to 'Complete task', complete_task_path(task), method: :put %></td>
      </tr>
    <% end %>
  </tbody>
</table>

控制器

def completed
  @tasks = Task.where(completed: true)
end

def complete
  @task.update_attribute :completed, true
  flash[:notice] = 'Task Completed'
  redirect_to completed_tasks_path
end

路线

resources :tasks do
  get 'completed', on: :collection
  put 'complete', on: :member
end

我认为您不需要该操作的视图,因为它重定向到 completed_tasks_path

【问题讨论】:

    标签: ruby-on-rails heroku routes


    【解决方案1】:

    尝试从 put 更改为 patch 看看是否可以解决问题?

    我尝试了该应用程序,重新创建了错误,但看不到您的代码有任何问题。也许你也可以试试这个:

    #config/routes.rb
    resources :tasks do
        member do
            patch :complete
        end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-12
      • 2018-09-16
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多