Billy Ferguson Billy Ferguson - 6 days ago 5
React JSX Question

Ruby on Rails with React getting NoMethodError

The following is my controller Projects#show method.

def show
@project = Project.find(params[:id])
@pm = User.find(@project.user_id)
@employees = {}
@supervisors = User.all.where("id = ?", Relationship.all.where("supervisor_id = ?", @project.user_id).pluck(:supervisor_id))
@supervisor_ids = Relationship.all.where("supervisor_id = ?", @project.user_id).pluck(:supervisor_id)
for supervisor in @supervisor_ids
@employees[supervisor] = User.all.where("id = ?", Relationship.all.where("supervisor_id = ?", supervisor).pluck(:employee_id))
end
end


The following is my Projects#show view html.erb file.

<%= react_component 'ProjectManage', { pm: @pm, supervisors: @supervisors, employees: @employees } %>


The following is my react front-end

@ProjectManage = React.createClass
getInitialState: ->
pm: @props.pm

render: ->
React.DOM.div
className: 'org-chart'
@props.pm.email
for supervisor in @props.supervisors
React.DOM.ul supervisor.email
for employee in @props.employees[supervisor]
React.DOM.li employee.email


The error message I get is:


Completed 500 Internal Server error in ActiveRecord.

NoMethodError (undefined method ` ' for ProjectsController#show:0x007fa195b59da0

Started GET "/projects/1" for 127.0.0.1 at 2016-11-24 06:31:38 -0800
Processing by ProjectsController#show as HTML Parameters:
{"id"=>"1"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]

Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE
"projects"."id" = ? LIMIT 1 [["id", 1]] User Load (0.2ms) SELECT
"users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]

(0.1ms) SELECT "relationships"."supervisor_id" FROM "relationships"
WHERE (supervisor_id = 1) User Load (0.1ms) SELECT "users".* FROM
"users" WHERE (id = 1) Completed 500 Internal Server Error in 17ms
(ActiveRecord: 0.5ms)

NoMethodError (undefined method '   ' for#): app/controllers/projects_controller.rb:11:in `show'

Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb
(2.7ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
(2.0ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb
within rescues/layout (19.6ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_markup.html.erb
(0.2ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_inner_console_markup.html.erb
within layouts/inlined_string (0.2ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb
within layouts/inlined_string (0.3ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/style.css.erb
within layouts/inlined_string (0.4ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/console.js.erb
within layouts/javascript (22.3ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/main.js.erb
within layouts/javascript (0.2ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.2ms) Rendered
/home/ferg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/web-console-2.3.0/lib/web_console/templates/index.html.erb
(34.9ms)

Answer

There were four spaces at the end of the @supervisors assignment. Removed trailing white space and the error is gone.

Comments