我想对我的一个嵌套模型执行编辑操作。我不断收到以下错误“NilClass:Class 的未定义方法‘model_name’”
我不知道发生了什么事。有什么帮助吗?
路线
resources :users, :except => [ :create, :new ] do
resources :store do
get "inbox", :on => :collection
get "openedmessages", :on => :collection
end
end
存储操作
def edit
@store = current_user.store.id
end
def create
@store = current_user.store.new(params[:store])
end
def update
@stores = current_user.store.id
if @stores.update_attributes
flash[:success] = "Store has been updated"
render 'edit'
else
render 'edit'
end
end
在商店中查看编辑操作
<ul class="storedashboard_header">
<li><%= link_to "Manage Gear", user_store_index_path(current_user) %></li>
<li><%= link_to "Store Appearance", edit_user_store_path(current_user, @store) %></li>
<li><%= link_to "Announcements", '#' %></li>
</ul>
<%= render "users/userdashboard_header" %>
<%= render "store/storemenu" %>
<div style="clear:both"></div>
<% flash.each do |name, msg| %>
<div class="alert alert-success">
<a class="close" data-dismiss="alert">×</a>
<%= msg %>
</div>
<% end %>
<div>
<%= simple_form_for @stores, :html => {:multipart => true, :class => 'form-horizontal'} do |f| %>
</br>
<div style="float: left;">
<%= f.input :storename %>
<%= f.input :storeimage %>
<%= f.input :storelogo %>
<%= f.button :submit, class: 'btn btn-large', style: 'margin-left: 40px;' %>
</div>
<% end %>
</div>
更新
将我的控制器更改为以下内容:
def edit
@store = Store.find(current_user.store.id)
end
def create
@store = current_user.store.new(params[:store])
end
def update
@store = Store.find(current_user.store.id)
if @store.update_attributes(params[:store])
flash[:success] = "Store has been updated"
render 'edit'
else
render 'edit'
end
end
更新后的新错误
#<#:0x007fec93b97238> 的未定义方法“store_path”
最终修复
需要修复视图...
<%= simple_form_for [current_user, @store], :html => {:multipart => true, :class => 'form-horizontal'} do |f| %>