将Devise current_user或user_id传递到React前端

时间:2019-02-20 00:51:15

标签: reactjs devise react-rails

尝试在提交新创建内容时将登录用户传递给我的React。将current_user替换为user_id会返回未定义的current_user,如预期的那样。我知道我需要先定义它,只是不确定如何做到这一点?

class UserLedgersIndex extends React.Component {
  constructor(props) {
    super(props);
    this.state = { user_ledgers: [], paginator: { count: 0, page: 0, limit: 0 } };
    this.handleCreate = this.handleCreate.bind(this);
    this.handleUpdate = this.handleUpdate.bind(this);
    this.handleChangePage = this.handleChangePage.bind(this);
  }

  componentDidMount() {
    this.fetchData(user_id, 1);
  }

  fetchData = (user_id, page_number) => {
    apiService.user_ledgersIndex(user_id, page_number)
      .then(
        paginated => {
          this.setState({
            user_ledgers: paginated.user_ledgers,
            paginator: {
              limit: paginated.meta.limit,
              count: paginated.meta.count,
              page: paginated.meta.page -1
            }
          });
        },
        error => {
          this.setState({ user_ledgers: [] });
        }
      );
  }

这是apiService:

const requestOptions = {
    method: 'POST',
    headers: Object.assign({},
      authorizationHeader(), 
      { 'Content_type': 'application/json' }),
    body: JSON.stringify({ ledger: { entry_type: entry_type }})
  };

  return fetch(`${process.env.API_SERVER}/api/v1/admin/${user_id}/user_ledgers`, requestOptions)
    .then(handleResponse)
    .then(user_ledger => {
      return user_ledger;
    });
}

0 个答案:

没有答案