mardi 26 mai 2015

Rails: NoMethodError, how do I show users profiles?

When I type at the top, /users/1 or /users/2 I get the same page yet there are 2 users. It just redirects me back to show.html.rb page. I'm not sure but maybe I don't have a page for each user. Maybe you can look at my code. I am unable to show <%= @user.name %>, I get a NoMethodError in Users#show. Maybe that is the problem...

show.html.rb

Avatar, <%= image_tag(current_user.avatar.url(:medium)) %>

Hello, <%= current_user.name %>

routes.rb

Rails.application.routes.draw do

   resources :products


   get 'users/index'
   get 'users/show'

   devise_for :users do
       resources :posts 
   end

   get 'users/:id' => 'users#show', as: :user


   resources :posts do
     member do
         get "like", to: "posts#upvote"
         get "dislike", to: "posts#downvote"


     end
     resources :comments
   end

   root 'posts#index'

end

I have each user in Show.html.rb and an index showing all the users at Index.html.rb

users_controller.rb

class UsersController < ApplicationController
    before_action :authenticate_user!

    def index
        @users = User.all
    end

    def edit
        @user = User.create( user_params )
    end

  def show
      @user = User.find_by_name(params[:id])
      @product = current_user.products.where(availability: true)
      @purchased = Sale.where(buyer_email: current_user.email)
      @sales = Sale.where(seller_email: current_user.email)
      @products = current_user.products
  end


 private

    def user_params
        params.require(:user).permit(:name, :email, :avatar)
    end

end

Aucun commentaire:

Enregistrer un commentaire