bottles
bottles

Reputation: 27

Clicking on a Username of another user links to my profile instead of the user's profile

I need help with this my code,that displays list of posts of users along with their username.What i want is, when i click on a username of a particular post, it should send me to that user's profile. instead, it send's to my own profile or the current user's profile whiles i want it to link me to the username i have clicked profile. (e.g like when you click on a username on Instagram, it links you to the user's profile so you can follow or unfollow and see their post) Please i need help. what i'm i not doing right in my code. i'm on rails 5.2.3 & ruby 2.3.3

Home


<div class="posts">
  <% @posts.each do |post| %>
    <section class="post">
      <div class="user">
        <div class="avatar">
      
          <img src="assets/avater.jpg">
          
        </div>
          <%= link_to post.user.username, user_path(post.user), class: 'username' %>
      </div>
      <%= image_tag post.image, class: 'main-image' %>
      <div class="description">
        <% post.description.to_s.split(' ').each do |word| %>
              <% if word.start_with?('#') %>
              <%= link_to word, search_index_path(query: word) %>
              <% else %>
                <%= word %>
                <% end %>
                <% end %>
      </div>
    </section>
  <% end %>
  <%= paginate @posts %>
</div>

routes

Rails.application.routes.draw do
  get 'search/index'
  devise_for :users
  get 'home/index'
    resources :posts
  root 'home#index'
  resources :users, only: [:show, :edit, :update]
  resources :posts, only: [:new, :create, :show, :destroy]
end

users controller

class UsersController < ApplicationController
    before_action :find_user
    

    def show
     @user  = User.find(params[:id])
     @posts = current_user.posts.order(created_at: :desc)
    end

    def edit
        
    end

    def update
      current_user.update(user_params)
      redirect_to current_user
    end

    private

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

    def user_params
      params.require(:user).permit(:username, :name, :website,:bio, :email, :phone, :gender, :avatar)
    end
end

post controller

class PostsController < ApplicationController

def new
  @post = current_user.posts.build
end

  def create
     @post = Post.create(post_params)
      redirect_to root_path
    end

    def show
      @post = Post.find(params[:id])
    end

    def destroy
      @post = current_user.posts.find(params[:id])
      @post.destroy

      redirect_to user_path(current_user)
    end

    private

    def post_params
      params.require(:post).permit(:description, :image, :user_id)
    end
end

home controller

class HomeController < ApplicationController
before_action :authenticate_user!

  def index
    @posts = Post.order(created_at: :desc).page(params[:page]).per(5)
  end
end

Upvotes: 0

Views: 57

Answers (1)

Jose Manuel
Jose Manuel

Reputation: 183

I guess the problem is that on the show method from users_controller, you are getting the posts from current_user instead of the user, it should be @posts = @user.posts.order(created_at: :desc)

Upvotes: 1

Related Questions