【发布时间】:2018-01-18 02:38:52
【问题描述】:
我将铁轨作为一种爱好,但我还是个新手,如果这听起来很荒谬,我深表歉意。我正在创建一个可以具有多种状态的板。每个状态可以有很多注释。但是,一旦我将注释循环添加到视图的状态循环中,就会出现此错误:
undefined method `notes' for nil:NilClass
boards/show.html.erb 文件的片段:
<% @board.statuses.each do |status| %>
<div>
<h2><%= link_to status.name, status_url(status)%></h2>
<% @status.notes.each do |note| %>
<h2><%= link_to notes.content, note_url(note)%></h2>
<% end %>
<%= link_to 'New notes', new_note_path(@note) %>
</div>
<% end %>
我不确定我是否在控制器或视图中做错了什么。我一直很难弄清楚。不过,我很感激任何帮助!
notes_controller:
class NotesController < ApplicationController
def new
@note = Note.new
end
def create
Note.create(note_params.merge(status_id: current_user.id))
redirect_to boards_url
end
def delete
Note.find(params[:id]).destroy(note_params)
end
def update
Note.find(params[:id]).update(note_params)
end
def note_params
params.require(:note).permit(:status_id, :content)
end
end
statuses_controller:
class StatusesController < ApplicationController
def new
@status = Status.new
end
def create
Status.create(status_params.merge(board_id: current_user.id))
redirect_to :root
end
def delete
Status.find(params[:id]).destroy(status_params)
end
def update
Status.find(params[:id]).update(status_params)
end
def show
@status = Status.find(params[:id])
end
def status_params
params.require(:status).permit(:board_id, :name)
end
end
如果需要更多信息,请告诉我。谢谢你。 :)
【问题讨论】:
标签: ruby-on-rails model-view-controller nested