调用方法后视图不显示

时间:2018-07-09 21:36:56

标签: ruby-on-rails ruby

我有一个projects.show.html页面,其中显示了一个项目及其指定的任务。如果任务的 completed 属性设置为 false (默认值),则会显示任务。我有一个将属性转换为 true 的方法,但是当它加载页面时,出现错误:

“ ActiveRecord :: RecordNotFound在ProjectsController#show中 找不到带有'id'= tasks

的项目
def show
        @project = Project.find(params[:id])
        @task = @project.tasks.build
    end

这是我的代码: projects_controller

   def show
    @project = Project.find(params[:id])
    @task = @project.tasks.build
   end

Tasks_controller

def completed
    @task = Task.find(params[:id])
    @task.completed = true
    @task.save
    redirect_to project_path(@project)
end

Projects / show.html.erb

<% if @project.tasks.size > 0 %>
<table class="table">
    <tr>
        <th>Task</th>
    </tr>
    <% @project.tasks.each do |task| %>
    <tr>
    <td><%= task.notes %></td>
    <td><% if !task.completed %><%= link_to "Mark Complete", 'tasks/#{:id}/completed' %></td>
    <% end %>
    <% end %>
    <% end %>
    </table>

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

这里tasks/#{:id}/completed-您将id设置为symbol,而您需要传递真实的id。您应该使用Rails的路由助手而不是tasks/#{:id}/completed,它将使您的生活更轻松。

  • 在终端中:rails routesrake routes
  • 找到所需的路线
  • 用诸如此类的link_to('My link', task_complete_path(task.id))
  • 替换您的链接

如果这对您没有帮助,请回复并提供您的路线定义。

答案 1 :(得分:0)

首先,考虑如下定义路线:

class book {...};
ostream& operator<<(ostream& os, genre g) { return os << genre_tbl[int(g)]; }

book b1;
b1.Gen = genre(0)
cout << b1.Gen;

(您可以在文档中详细了解nesting resourcesadding more RESTful actions。)

哪个会给你:

resources :projects do 
  resources :tasks, shallow: true do 
    member do 
      patch :completed
    end
  end
end

请注意,您现在拥有 completed_task PATCH /tasks/:id/completed(.:format) tasks#completed project_tasks GET /projects/:project_id/tasks(.:format) tasks#index POST /projects/:project_id/tasks(.:format) tasks#create new_project_task GET /projects/:project_id/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 projects GET /projects(.:format) projects#index POST /projects(.:format) projects#create new_project GET /projects/new(.:format) projects#new edit_project GET /projects/:id/edit(.:format) projects#edit project GET /projects/:id(.:format) projects#show PATCH /projects/:id(.:format) projects#update PUT /projects/:id(.:format) projects#update DELETE /projects/:id(.:format) projects#destroy 的命名路径,该路径可以响应completed_task HTTP动词。

然后,将PATCH更新为包含以下内容:

app/views/projects/show.html.erb

在我看来,您的表格格式错误,因此我尝试对其进行修复。您可能需要摆弄。

您的TasksController#completed动作也格式错误。您尝试重定向到<% if @project.tasks.any? %> <table class="table"> <tr> <th>Task</th> </tr> <% @project.tasks.each do |task| %> <tr> <td> <%= task.notes %> </td> <td> <% unless task.completed %> <%= link_to "Mark Complete", completed_task_path(task), method: :patch %> <% end %> </td> </tr> <% end %> </table> <% end %> ,但从未定义project_path(@project)。它看起来应该更像:

@project

自然,这假设您已经正确设置了Task和Project关联,例如:

def completed
  @task = Task.find(params[:id])
  @task.update(completed: true)
  redirect_to @task.project
end

仅供参考,我只是克隆了您的项目,并将其添加到class Task < ActiveRecord::Base belongs_to :project end 中(自然,由于数据库中没有任何记录,因此'1'只是一个虚拟ID):

views/static/index.html.erb

哪个给了我

<%= link_to "Test", completed_task_path(1), method: :patch %>

单击它会得到:

<a rel="nofollow" data-method="patch" href="/tasks/1/completed">Test</a>

答案 2 :(得分:0)

想通了,谢谢@jvillian的帮助。

我没有在我的任务控制器中使用方法来更改Task.completed属性(最初是字符串),而是创建了一个可枚举(类似于布尔值的二进制形式),可以将其从“不完整”切换为“完整” “无论何时调用该方法。

app / controllers / tasks_controller:

include ProjectsHelper

class TasksController < ApplicationController

def create
    t = Task.new(task_params)
    t.save
    redirect_to project_path(t.project)
end

def update
    t = Task.find(params[:id])
    if t.user == current_user
        t.update(completion_status: 'complete', completion_date: DateTime.now, completed: true)
        t.project.update(last_completed: DateTime.now)
    end
    redirect_to :back
end

def toggle_status
    @task = Task.find(params[:id])

    if @task.incomplete?
        @task.complete!
    elsif @task.complete?
        @task.incomplete!
    end

    redirect_to @task.project
  end


private

def task_params
    params.require(:task).permit(:user_id, :project_id, :notes)
end

然后针对我的模型:

class Task < ApplicationRecord
    enum status: [ :incomplete, :complete ]

belongs_to :user
belongs_to :project

validates :notes, presence: true

scope :completed, -> { where(completed: true) }

def self.completed
    Task.completed
end
end

枚举状态用作二进制选项,非常类似于布尔值。我以前是一个字符串,我能够更改方法来简单地“翻转”可枚举的值,并在我的项目/节目中编写适当的控制器动作:

<% if @project.tasks.any? %>
  <table class="table">
    <tr>
      <th>Task</th>
    </tr>
    <% @project.tasks.each do |task| %>
      <tr>
        <td>
          <%= task.notes %>
          <%= task.status %>
        </td>
        <td>
        <% if !task.id.nil? %>

            <%= link_to "Change Status", toggle_status_task_path(task) %>
        <% end %>
        </td>
      </tr>
    <% end %>

  </table>
<% end %>