samedi 29 août 2015

Route in rails is not working

i have used devise gem for authentication.in same project i have used album model. but my new_user_album_path is giving error.i think there is user id problem.please help me.

this is index.html.erb file

<%= link_to 'New Article', new_user_album_path() %>
<table>
<tr>
 <th>Title</th>
<th>Description</th>
 </tr>

  <% @albums.each do |album| %>
  <tr>
  <td><%= album.title %></td>
    <td><%= album.description %></td>
  <td><%= link_to 'Show', user_album_path(album.user,album) %></td>
 <td><%= link_to 'Edit', edit_user_album_path(album.user,album) %></td>
  <td><%= link_to 'destroy', user_album_path(album.user,album),
    method: :delete,
    data: { confirm: 'Are you sure?' } %></td>
   </tr>

   <% end %>
   </table>



     this is albums_controller file

   class AlbumsController < ApplicationController
     before_action :authenticate_user!

   def index
    @albums = current_user.albums.all

       end

        def show
         @album = current_user.albums.find(params[:id])
       end

    def new
    @album = current_user.albums.new
   end

    def edit
    @album = current_user.albums.find(params[:id])
    end

   def create

   @album = current_user.albums.build(album_params)
   if @album.save
   redirect_to action: 'index'
   else
      render 'new'
   end
  end

     def update
    @album = current_user.albums.find(params[:id])

      if @album.update(album_params)
      redirect_to action: 'show'
      else
      render 'edit'
      end
     end

     def destroy
      @album = current_user.albums.find(params[:id])
      @album.destroy
     redirect_to action: 'index'
     end
    private
    def album_params
      params.require(:album).permit(:title, :description, :user_id)
    end

   end

   it is giving following errror.->


       NoMethodError in Albums#index



    undefined method `users' for nil:NilClass

    Extracted source (around line #2):

       <h1>Your Albums</h1>
    <%= link_to 'New Article', new_user_album_path(@album.users.id) %>
       <table>
       <tr>
          <th>Title</th>
        <th>Description</th>

route.rb file

    devise_for :users
   # The priority is based upon order of creation: first created priority.
 # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
   resources :users do
   resources :albums
   end

   root 'albums#index'

Aucun commentaire:

Enregistrer un commentaire