samedi 27 février 2016

NameError in Users#show - Rails

I am following Michael Hurtl's Rails Tutorial and In Chapter 11 When i want to view the profile page of the User i get this error :

NameError in Users#show undefined local variable or method `micropost' and This line of Code is highlighted in red :

<%= link_to micropost.user.name , micropost.user%>

Here's My code:

microposts_controller.rb

class MicropostsController < ApplicationController
        before_action :authenticate_user! , only: [:create,:destroy]

    def create
        @micropost = current_user.microposts.build(micropost_params)

        if @micropost.save
            flash[:success] = "Micropost created"
            redirect_to root_url
        else
            render 'pages/home'
        end
    end

    def destroy
    end

    private

    def micropost_params
        params.require(:micropost).permit(:content)
    end
end

users_controller.rb

class UsersController < ApplicationController
    before_action :authenticate_user! , only: [:edit,:update,:destroy]

    def index
        @users = User.all
    end

    def show
        @user = User.find(params[:id])
        @microposts = @user.microposts
    end

end

microposts/_microposts.html.erb

<%= link_to micropost.user.name , micropost.user%>

Aucun commentaire:

Enregistrer un commentaire