首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails中嵌套模型编辑页的错误

Rails中嵌套模型编辑页的错误
EN

Stack Overflow用户
提问于 2012-12-16 00:33:53
回答 1查看 77关注 0票数 0

我正在绑定以对我的嵌套模型执行编辑操作。我一直得到以下错误“NilClass的未定义方法`model_name‘:Class”

我不知道怎么回事。有什么帮助吗?

路由

代码语言:javascript
复制
  resources :users, :except => [ :create, :new ] do
     resources :store do
       get "inbox", :on => :collection
       get "openedmessages", :on => :collection
     end
  end

存储操作

代码语言:javascript
复制
  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

存储中编辑操作的视图

代码语言:javascript
复制
<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>

更新

将我的主计长改为:

代码语言:javascript
复制
  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

更新后的新错误

用于`store_path的未定义方法#<#:0x007fec93b97238>‘

最终修复

需要修复景观..。

代码语言:javascript
复制
    <%= simple_form_for [current_user, @store], :html => {:multipart => true, :class => 'form-horizontal'} do |f| %>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-16 01:27:13

如果我能正确理解你的问题:

@store需要成为模型存储的对象,而不仅仅是id。

类似于:@store = Store.find(current_user.store.id)将返回对象

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13897507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档