我无法弄清楚如何显示索引。
在我的组织请求视图文件夹中,我有一个名为index.html.erb的文件。
在该文件中,我正在尝试列出每个组织请求。我尝试了以下每种配方:
<% OrganisationRequest.each do |OrgReq| %>
<% organisation_request.each do |OrgReq| %>
<% @organisation_request.each do |OrgReq| %>
<% @organisation_requests.each do |OrgReq| %>
在每种情况下,我都会收到错误消息:
formal argument cannot be a constant
我认为一个常数意味着以大写字母开头的东西。以上3次尝试不以大写字母开头。
这也让我感到困惑,因为在我的用户索引中,我有&lt;%User.each%&gt;我没有收到错误消息。
有人能看出出了什么问题吗?我如何要求提供对象列表?
答案 0 :(得分:0)
如果您的数据和视图正确,您应该能够修复:
import React from 'react'
import { render } from 'react-dom'
import { Router, Route, browserHistory, IndexRedirect} from 'react-router'
import {isInteger} from 'lodash'
let _data = {};
let _changeListeners = [];
render((
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRedirect to="/locations/random"/>
<Route path="/locations/:locationId" component={Dashboard}/>
</Route>
</Router>
), document.getElementById('app'));
const App = React.createClass({
render() {
return <div>Home</div>
}
});
const Dashboard = React.createClass({
getInitialState: function () {
return {
data: {}
}
},
updateData(){
// Once the LocationStore has sent out a notification that the data has changed, trigger a render of the dashboard
this.setState({
data: LocationStore.getData()
});
},
shouldComponentUpdate (nextProps) {
// Trigger a request to load new data when the url changes
LocationStore.getLocation(nextProps.params);
return true;
},
componentDidMount() {
LocationStore.addChangeListener(this.updateData);
},
componentWillUnmount() {
LocationStore.removeChangeListener(this.updateData);
},
render() {
return (
<div>
<div className="page-container" style={{ paddingBottom: 20}}>
<div className="col-lg-12">
<h2>Display Data Here as a Row</h2>
</div>
</div>
</div>
)
}
});
const LocationStore = {
getLocation: function (params) {
// Get location by id or random
const isRandom = !isInteger(params.locationId);
var url = (isRandom) ? "/api/locations/random" : `/api/locations/${params.locationId}`;
Utils.getJSON(url, function (err, res) {
_data = res;
// If the user hit the random route, update the URL here to show the id of the record brought down
if (isRandom) {
const path = `/locations/${_data.id}`;
browserHistory.push(path);
}
LocationStore.notifyChange();
})
},
getData: function () {
return _data;
},
notifyChange: function () {
_changeListeners.forEach(function (listener) {
listener()
})
},
addChangeListener: function (listener) {
_changeListeners.push(listener)
},
removeChangeListener: function (listener) {
_changeListeners = _changeListeners.filter(function (l) {
return listener !== l
})
}
};
const Utils = {
getJSON: function (url, cb) {
const req = new XMLHttpRequest();
req.onload = function () {
if (req.status === 404) {
cb(new Error('not found'))
} else {
cb(null, JSON.parse(req.response))
}
};
req.open('GET', url);
req.send()
}
};
答案 1 :(得分:0)
如果我们坚持Rails惯例,我们会说,你有一个OrganisationRequests控制器,有这样的内容。
class OrganisationRequestsController < ApplicationController
...
def index
@your_local_variable = OrganisationRequest.find(...)
end
...
end
也就是说,您需要在视图文件中使用@your_local_variable
。
<% @your_local_variable.each do |o| %>
....
<% end %>
如果索引操作中的变量是@organisation_requests
,请使用它。