单击另一个用户的用户名链接到我的个人资料,而不是该用户的个人资料

时间:2019-05-06 13:09:37

标签: ruby-on-rails ruby

我需要我的代码的帮助,该代码显示用户的帖子列表以及用户名。我想要的是,当我单击特定帖子的用户名时,它应该将我发送到该用户的个人资料。相反,它将发送到我自己的个人资料或当前用户的个人资料,而我希望它将我链接到我单击个人资料的用户名。 (例如,当您单击Instagram上的用户名时,它会将您链接到用户的个人资料,以便您可以关注或取消关注并查看他们的信息) 请我需要帮助。我在代码中做的不正确。 我在轨道上5.2.3和红宝石2.3.3

首页


<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>

路线

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

用户控制器

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

后置控制器

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

家庭控制器

class HomeController < ApplicationController
before_action :authenticate_user!

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

1 个答案:

答案 0 :(得分:1)

我想问题是,在public static MyEvent getMostRecentLastEventOfSequence( SortedSet<MyEvent> events, List<String> sequence) { String first = sequence.get(0), second = sequence.get(1); UnaryOperator<MyEvent> previous; Stream<MyEvent> stream; if (events instanceof NavigableSet) { NavigableSet<MyEvent> navigableSet = (NavigableSet<MyEvent>) events; previous = navigableSet::lower; stream = navigableSet.descendingSet().stream(); } else { previous = event -> events.headSet(event).last(); stream = Stream.iterate(events.last(), previous).limit(events.size()); } return stream .filter(event -> event.getAbbreviation().equals(second)) .filter(event -> { MyEvent p = previous.apply(event); return p != null && p.getAbbreviation().equals(first); }) .findFirst().orElse(null); } 的{​​{1}}方法上,您是从current_user而不是从用户那里获取帖子的,它应该是show