Rails未定义变量@assets

时间:2016-01-28 12:08:00

标签: ruby-on-rails

我正在关注此tutorial


我的错误是我在_form.html.erb中使用的@asset变量,而index.html.erb无效。它是说表单中的第一个参数不能包含nil或为空 。但当我更换它时:asset_path然后它开始工作
[assets_controller]

class AssetsController < ApplicationController
   before_filter :authenticate_user!  #authenticate for users before any methods is called 


  def index 
    @assets = current_user.assets      
  end



  def new
    @asset = current_user.assets.new
  end

  def create 
    @asset = current_user.assets.new(user_assets) 

  end




private
    def user_assets
         params.require(:asset).permit(:user_id, :uploaded_file)
    end

end


[资产/ _form.html.erb]

<%= form_for @assets, :html => {:multipart => true} do |f| %> 
  <%= f.error_messages %> 

    <p> 
        <%= f.label :uploaded_file, "File" %><br /> 
        <%= f.file_field :uploaded_file %> 
      </p> 
  <p><%= f.submit "Upload" %></p> 
<% end %>


[index.html.erb]

  <% title "Assets" %> 
    <table> 
      <tr> 
        <th>Uploaded Files</th> 
      </tr> 

      <% for asset in @assets %> 

        <tr> 
          <td><%= link_to asset.uploaded_file_file_name, asset.uploaded_file.url %></td> 
          <td><%= link_to "Show", asset %></td> 
          <td><%= link_to "Edit", edit_asset_path(asset) %></td> 
          <td><%= link_to "Destroy", asset, :confirm => 'Are you sure?', :method => :delete %></td> 
        </tr> 
      <% end %> 
    </table> 
    <p><%= link_to "New Asset", new_asset_path %></p>


[rake routes]

new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session GET    /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                    root GET    /                              homes#index
                  assets GET    /assets(.:format)              assets#index
                         POST   /assets(.:format)              assets#create
               new_asset GET    /assets/new(.:format)          assets#new
              edit_asset GET    /assets/:id/edit(.:format)     assets#edit
                   asset GET    /assets/:id(.:format)          assets#show
                         PATCH  /assets/:id(.:format)          assets#update
                         PUT    /assets/:id(.:format)          assets#update
                         DELETE /assets/:id(.:format)          assets#destroy

1 个答案:

答案 0 :(得分:2)

#assets/_form.html.erb
<%= form_for @asset, :html => {:multipart => true} do |f| %> 

如果使用form_for,则必须传递有效的对象类型。通常,这是通过变量(如上所述)完成的,但它可以使用符号。

form_for方法从这个初始值推断出其他表单属性(path等),因此你需要有一个初始值。

-

您遇到的问题是,对于方法,您已声明@asset,但在@assets中使用了form_for。遗憾的是标准的未声明/零变量问题。