0

ネストされたモデルの 1 つで編集アクションを実行しようとしています。「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| %>
4

1 に答える 1

1

私があなたの質問を正しく理解していれば:

@store は、id だけでなくモデル Store のオブジェクトである必要があります。

次のようなもの: @store = Store.find(current_user.store.id) はオブジェクトを返します

于 2012-12-16T01:27:13.010 に答える