【问题标题】:Why is wrong redirecting?为什么重定向错误?
【发布时间】:2017-12-26 10:00:14
【问题描述】:

让模型包含他们已完成和待办的任务。在视图中具有链接,当单击该链接时,它必须将任务从待办事项更改为已完成。这是我的路线

get 'tasks/:id', to: 'tasks#change_to_done', as: 'change_to_done'

我的看法

<% unless task.done %>
        <td><%= check_box_tag "cb_tasks[]", task.id %></td>
        <td><%= link_to task.title, task %></td>
        <td><%= link_to 'Edit', edit_task_path(task.id) %></td>
        <td><%= link_to 'Done', change_to_done_path(task.id) %></td>
        <td><%= link_to 'Destroy', task, method: :delete, data: {confirm: 'Are you sure?'} %></td>
      <% end %>

和我的控制器

  def change_to_done
    @task = Task.find(params[:id])
    @task.done = true
    @task.save
  end

当点击链接重定向到显示路径时

Started GET "/tasks/32" for 127.0.0.1 at 2017-12-26 11:59:06 +0200
Processing by TasksController#show as HTML
  Parameters: {"id"=>"32"}
  Task Load (0.1ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT ?  [["id", 32], ["LIMIT", 1]]
  Rendering tasks/show.html.erb within layouts/application
  Rendered tasks/show.html.erb within layouts/application (0.7ms)
Completed 200 OK in 57ms (Views: 55.0ms | ActiveRecord: 0.1ms)

怎么了?我只需要将字段状态从 false 更改为 true。

【问题讨论】:

    标签: ruby-on-rails ruby web model-view-controller


    【解决方案1】:

    Rails 路由按照指定的顺序进行匹配,因此如果您在 get 'task/:id' 上方有一个 resources :tasks,则资源行的 show 操作路由将在 get 行之前匹配。

    尝试改变端点,例如:

    get 'change_to_done/:id', to: 'tasks#change_to_done', as: 'change_to_done'
    

    【讨论】:

      【解决方案2】:

      您的路由文件中可能包含resources :tasks。 Rails 解决了这个问题,并忽略 get 'tasks/:id', to: 'tasks#change_to_done', as: 'change_to_done' 这条路线。您应该指定确切的操作get 'tasks/:id/change_to_done', to: 'tasks#change_to_done', as: 'change_to_done'

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-12
        • 1970-01-01
        • 2013-05-20
        • 2014-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多