我想在我的一个嵌套模型上执行编辑操作。我一直收到以下错误“NilClass的未定义方法`model_name':Class”
我不知道发生了什么事。有什么帮助吗?
路线
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
更新后的新错误
#&lt;#:0x007fec93b97238&gt;
的未定义方法`store_path'最终修正
需要修复视图......
<%= simple_form_for [current_user, @store], :html => {:multipart => true, :class => 'form-horizontal'} do |f| %>
答案 0 :(得分:1)
如果我理解你的问题:
@store需要是模型商店的对象,而不仅仅是id。
像这样的东西: @store = Store.find(current_user.store.id)将返回对象