简体   繁体   English

使用rails获取当前用户的配置文件并做出反应

[英]Get current user's profile with rails and react

I'm finally taking the leap and learning React with Rails. 我终于实现了飞跃并学习了Ract的React。 Currently at the basics and need some help showcasing the current logged in users profile link. 目前在基础知识和需要一些帮助展示当前登录用户配置文件链接。 I understand the process of doing this just need help with execution. 我理解这样做的过程只需要执行帮助。

Before, a link to the currentUser's profile with erb was like this 以前,使用erb链接到currentUser的配置文件是这样的

<li><%= link_to 'Profile', current_user %></li>

I'm now rendering my 'Navbar' component like so 我现在正在渲染我的'Navbar'组件

<%= react_component 'Navbar' %>

and this is the actual component: 这是实际的组成部分:

var Navbar = React.createClass({
  getInitialState() {
      return {
      };
  },
  getDefaultProps() {
      return {
          siteTitle: "React Test"  
      };
  },
  render: function() {
    return(
      <div className="navi">
        <div className="logo">
          <h1>{this.props.siteTitle}</h1>
        </div>
        <ul className="nav">
          <li className="item">
            <a href="#" className="hvr-float-shadow">Profile</a>
          </li>
        </ul>
      </div>
    )
  }
});

How do I set the current user id in the component state. 如何在组件状态中设置当前用户标识。

Use props to pass in data: 使用props传入数据:

<%= react_component('Navbar', current_user: current_user)  %>

Assuming your profile path is something like: 假设您的个人资料路径类似于:

localhost:3000/profile/:id

Then your react would be: 然后你的反应是:

var Navbar = React.createClass({
  getInitialState() {
    return {
      currentUser: this.props.current_user
    };
  render: function() {
    return (
      <ul>
        <li>
          <a href={"localhost:3000/profile/"+ this.state.currentUser.id}>Profile</a>
        <li>
      <ul>
    );
  }
},

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

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