控制器方法Rails 5

时间:2018-09-19 14:35:15

标签: ruby ruby-on-rails-5 nomethoderror

编辑#2

这是我单击对勾时的服务器日志

Started PUT "/course_modules/module-1-understanding-email-marketing-basics/complete" for 127.0.0.1 at 2018-09-19 16:29:42 +0100
Processing by CourseModulesUsersController#complete as HTML
  Parameters: {"authenticity_token"=>"CfIM56PhiIRumytV99CE7GFK6tYumCgDvBwUS79jhbCr3zpXLfbJgvqUFkzwmKLXQiOxk1SmFvGoO4+M1z87wg==", "id"=>"module-1-understanding-email-marketing-basics"}
  CourseModulesUser Load (0.7ms)  SELECT  "course_modules_users".* FROM "course_modules_users" WHERE "course_modules_users"."id" = $1 LIMIT $2  [["id", 0], ["LIMIT", 1]]
  ↳ app/controllers/course_modules_users_controller.rb:3
Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.7ms)



NoMethodError (undefined method `complete=' for nil:NilClass):

app/controllers/course_modules_users_controller.rb:4:in `complete'

编辑#1

这就是我现在的位置。

我可以使用Rails控制台访问记录

irb(main):017:0> @course_modules_users = CourseModulesUser.find_by(id: 1)
  CourseModulesUser Load (0.4ms)  SELECT  "course_modules_users".* FROM "course_modules_users" WHERE "course_modules_users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
=> #<CourseModulesUser id: 1, course_module_id: 1, courses_user_id: 1, complete: false, created_at: "2018-09-19 13:04:16", updated_at: "2018-09-19 14:58:17">
irb(main):018:0> @course_modules_users.complete = true
=> true

如果我尝试根据这样的布尔值进行检查

<% if @course_modules_users.where(complete: true).nil? %>
  <i class="fas fa-check text-green float-left mr-1"></i>
  <span class="text-xs mr-2">Completed</span>
<% else %>
  <%= link_to complete_course_module_path(course_module), method: :put do %>
  <i class="fas fa-check text-grey-darkest float-left mr-2"></i>
  <% end %>
<% end %>

显示为

image

但是,如果我手动更改记录

image

前端没有任何变化

image

原始

这是我的错误:

image

因此,我目前有一个路由设置,一旦单击该路由,就会触发以下complete方法,并将记录更改为true。但是,如您所见,它找不到完整的方法。

这是数据库

database

我已经在终端中对此进行了测试,它可以找到完整的字段

termial

1 个答案:

答案 0 :(得分:2)

该错误表明/*div and interior BG*/ div { width: 80%; height: 300px; /*Make sure your content has a height specified*/ display: inline-block; background: linear-gradient(290deg, blue 50%, darkblue 50%); position: relative; margin-left: 10%; } /*Shared styles across pseudo elements*/ div:before, div:after { content: ''; width: 20%; min-height: 300px; /*Fits psuedo element height to content*/ position: absolute; display:inline-block; } /*Position and cuts for left side*/ div:before { left: -9.9%; background: linear-gradient(135deg, transparent 15px, darkblue 0) top left, linear-gradient(45deg, transparent 15px, darkblue 0) bottom left; background-size: 100% 51%; background-repeat: no-repeat; } /*Position and cuts for left right*/ div:after { right: -9.9%; background: linear-gradient(-135deg, transparent 15px, blue 0) top right, linear-gradient(-45deg, transparent 15px, blue 0) bottom right; background-size: 100% 51%; background-repeat: no-repeat; } nil ,指出@course_modules_user方法有误。这样就定义了

complete

检查@course_modules_user = CourseModulesUser.find_by(id: params[:id]) 并确保您在params表中有一个带有params[:id]值的记录,以使course_modules_users不为空。

更新

在使用@course_modules_user时,friendly_id的值将以id的形式发送。因此,请像这样更改slug来告诉Rails不要这么做

link_to