简体   繁体   English

Ruby on rails with React Frontend(显示相关数据)

[英]Ruby on rails with React Frontend(Displaying associated data)

I been trying show association between users and posts tables in rails.我一直在尝试在 Rails 中显示用户和帖子表之间的关联。 My problem is that when a user logs in, he/she should be able to see all of their own posts in my react frontend.我的问题是当用户登录时,他/她应该能够在我的反应前端看到他们自己的所有帖子。 However, my frontend request is only able to fetch first record that is related to my current user.但是,我的前端请求只能获取与我当前用户相关的第一条记录。 This is where I send a fetch request to my backend to get the posts that are related to the user's id.这是我向后端发送获取请求以获取与用户 ID 相关的帖子的地方。

export default function Profile({currentUser}){

    const [posts, setPosts] = useState([])

     useEffect(() => {
        fetch(`/posts/${currentUser.id}`)
        .then((r) =>{
          if(r.ok){
              r.json().then((posts)=>setPosts(posts))
          }
      })
    }, [])

And this is my how my route looks like这就是我的路线的样子

 get '/posts/:id', to: "posts#show"

Lastly, this is where my backend fetches blog posts that are related to the logged in user.最后,这是我的后端获取与登录用户相关的博客文章的地方。

  def show
     posts = Post.find_by(id:params[:id])
     render json: posts, include: :user
  end

I know the fact that find_by method only fetches the first record that meets the condition.我知道 find_by 方法只获取满足条件的第一条记录。 I also tried using user.Post.all to fetch the records.我还尝试使用user.Post.all来获取记录。 Any advices?有什么建议吗?

Currently your request will return the Post with the :id of your currentUser .目前,您的请求将返回带有您currentUser:idPost I think that's not what you want... :)我认为这不是你想要的...... :)

I guess you want something like:我猜你想要类似的东西:

def show
   posts = User.find(params[:id]).posts # Hint: find_by(id: id) == find(id)
   ...
end

You are using the routes, the controller, and the request in a weird way.您正在以一种奇怪的方式使用路由、控制器和请求。

Problems问题

I assume the controller you shared is the Posts Controller, that means you need the Index action, not the Show action.我假设您共享的控制器是 Posts 控制器,这意味着您需要Index操作,而不是 Show 操作。 The Show action is used when you want to render a single Post.当您想要呈现单个 Post 时使用Show操作。

You pass the currentUser.id to the backend as posts/:id .您将currentUser.id作为posts/:id传递给后端。 I'm afraid that's not right as the posts/:id refers to a Post id not a User id.恐怕这是不对的,因为posts/:id指的是 Post id 而不是 User id。 Apart from that, your Backend should be already aware of the User as it is logged in.除此之外,您的后端应该在用户登录时已经知道它。

Your authorization gem should have a way to access the current User.您的授权 gem 应该有一种访问当前用户的方法。 For example, the devise gem exposes has a method called current_user to all the controllers.例如,devise gem 向所有控制器公开了一个名为current_user的方法。

Solution解决方案

That means your route should be get '/posts', to: "posts#index"这意味着您的路线应该是get '/posts', to: "posts#index"

Your controller should be你的控制器应该是

  def index
     posts = current_user.posts # current_user or your way to access the user
     render json: posts, include: :user
  end

Your React front-end should be你的 React 前端应该是

export default function Profile({currentUser}){

    const [posts, setPosts] = useState([])

     useEffect(() => {
        fetch(`/posts`)
        .then((r) =>{
          if(r.ok){
              r.json().then((posts)=>setPosts(posts))
          }
      })
    }, [])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM