Rails undefined variable @assets -
i following tutorial
error @asset variable using in _form.html.erb , index.html.erb not working .it saying first argument in form cannot contain nil or empty .but when replace :asset_path starts working
[assets_controller]
class assetscontroller < applicationcontroller before_filter :authenticate_user! #authenticate users before methods 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
[assets/_form.html.erb]
<%= form_for @assets, :html => {:multipart => true} |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> <% 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 sure?', :method => :delete %></td> </tr> <% end %> </table> <p><%= link_to "new asset", new_asset_path %></p>
[rake routes]
new_user_session /users/sign_in(.:format) devise/sessions#new user_session post /users/sign_in(.:format) devise/sessions#create destroy_user_session /users/sign_out(.:format) devise/sessions#destroy user_password post /users/password(.:format) devise/passwords#create new_user_password /users/password/new(.:format) devise/passwords#new edit_user_password /users/password/edit(.:format) devise/passwords#edit patch /users/password(.:format) devise/passwords#update put /users/password(.:format) devise/passwords#update cancel_user_registration /users/cancel(.:format) devise/registrations#cancel user_registration post /users(.:format) devise/registrations#create new_user_registration /users/sign_up(.:format) devise/registrations#new edit_user_registration /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 / homes#index assets /assets(.:format) assets#index post /assets(.:format) assets#create new_asset /assets/new(.:format) assets#new edit_asset /assets/:id/edit(.:format) assets#edit asset /assets/:id(.:format) assets#show patch /assets/:id(.:format) assets#update put /assets/:id(.:format) assets#update delete /assets/:id(.:format) assets#destroy
#assets/_form.html.erb <%= form_for @asset, :html => {:multipart => true} |f| %>
if use form_for
, have pass valid object type. typically, done variable (as above), can symbol.
the form_for
method infers other form attributes (path
etc) initial value, hence need have initial value it.
--
the issue have new method, you've declared @asset
, yet using @assets
in form_for
. standard undeclared / nil variable problem unfortunately.
Comments
Post a Comment