用户编辑视图出现奇怪的错误
当我在 index
视图中单击此链接时:
<%= link_to "Edit Password", edit_user_path(current_user) %>
我收到此错误:
NoMethodError in Users#edit
Showing /rubyprograms/dreamstill/app/views/videos/_modal.html.erb where line #3 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #3):
1: <div id="boxes">
2: <div id="dialog" class="window">
3: <%= form_for(@video) do |f| %>
这与我渲染到 index_modal
的部分有关代码>视图。它有一个形式在里面。
我的视频控制器中也有这个:
def index
@video = Video.new
@videos = Video.paginate(:page => params[:page], :per_page => 20)
end
为什么我会收到此错误,如何修复它?
更新:
这是我在用户控制器中的编辑操作:
def edit
@user = current_user
end
这是 _modal
部分:
<div id="boxes">
<div id="dialog" class="window">
<%= form_for(@video) do |f| %>
<% if @video.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@video.errors.count, "error") %> prohibited this video from being saved:</h2>
<ul>
<% @video.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :video_url %><br />
<%= f.text_field :video_url %>
</div>
<div class="field">
<%= f.label :title, 'Song Title' %><br />
<%= f.text_field :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Cancel', '#', :class => 'close' %>
</div>
<div id="mask"></div>
</div>
When I click on this link in my index
view:
<%= link_to "Edit Password", edit_user_path(current_user) %>
I get this error:
NoMethodError in Users#edit
Showing /rubyprograms/dreamstill/app/views/videos/_modal.html.erb where line #3 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #3):
1: <div id="boxes">
2: <div id="dialog" class="window">
3: <%= form_for(@video) do |f| %>
This has to do with a partial called _modal
that I render into the index
view. It has a form in it.
I also have this in my Videos controller:
def index
@video = Video.new
@videos = Video.paginate(:page => params[:page], :per_page => 20)
end
Why am I getting this error, and how can I fix it?
UPDATE:
Here's my edit action in the Users controller:
def edit
@user = current_user
end
Here's the _modal
partial:
<div id="boxes">
<div id="dialog" class="window">
<%= form_for(@video) do |f| %>
<% if @video.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@video.errors.count, "error") %> prohibited this video from being saved:</h2>
<ul>
<% @video.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :video_url %><br />
<%= f.text_field :video_url %>
</div>
<div class="field">
<%= f.label :title, 'Song Title' %><br />
<%= f.text_field :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Cancel', '#', :class => 'close' %>
</div>
<div id="mask"></div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您遇到的问题基本上与上次相同。
由于您正在有效地渲染
edit
操作(无论它是否在模态中),因此您需要再次定义@video
。I believe you are basically having the same problem as last time.
Since you are effectively rendering the
edit
action—whether it is in a modal or not—you need@video
defined again.