我有一个看起来像这样的循环
struct Flight_Details {
char destination[99];
char departure[99];
char time_depart[80];
char time_arrive[80];
int flight_number;
};
switch (menu)
{
case MENU::NEW_FLIGHT: {
Flight_Details flight_d;
cout << "Enter Departure: ";
cin >> flight_d.destination;
cout << "Enter Destination: ";
cin >> flight_d.departure;
cout << "Enter Departure Time: ";
cin >> flight_d.time_depart;
cout << "Please enter arriving time: ";
cin >> flight_d.time_arrive;
cout << "Flight Number: ";
cin >> flight_d.flight_number;
ofstream file;
file.open("Flgiht_Details.data", ios::binary);
if (!file) cout << "Could create/open file";
else {
file.write((char*)&flight_d, sizeof(flight_d));
file.close();
}
break;
}
case MENU::OUTPUT_FILE: {
ifstream readFile;
readFile.open("Flgiht_Details.data");
if (!readFile) cout << "Couldn't open file";
else {
readFile.seekg(0, ios::end);
int fileSize = readFile.tellg();
int countOfFlights = fileSize / sizeof(Flight_Details);
readFile.seekg(0, ios::beg);
Flight_Details* flight = new Flight_Details[countOfFlights];
readFile.read((char*)flight, countOfFlights *sizeof(Flight_Details));
readFile.close();
for (int i = 0; i < countOfFlights; i++)
{
cout << flight[i].destination << "\n" << flight[i].departure << "\n" << flight[i].time_depart << "\n" << flight[i].time_arrive << "\n" << flight[i].flight_number << "\n\n";
}
break;
}
}
case MENU::EDIT: {
Flight_Details* flight_d;
ifstream readFile;
readFile.open("Flgiht_Details.data");
if (!readFile) cout << "Couldn't open file";
else {
readFile.seekg(0, ios::end);
int fileSize = readFile.tellg();
int countOfFlights = fileSize / sizeof(Flight_Details);
readFile.seekg(0, ios::beg);
Flight_Details* flight = new Flight_Details[countOfFlights];
readFile.read((char*)flight, countOfFlights * sizeof(Flight_Details));
readFile.close();
ofstream file;
char edit[50];
cout << "Edit: ";
cin.getline(edit, 50);
for (int i = 0; i < countOfFlights; i++)
{
if (strcmp(flight_d[i].destination, edit) == 0)
{
//edit file
}
}
}
}
}
完美地显示属于用户的集合,然后显示每个集合中有多少个故事。
但是,我想使用这样做的助手。
视图中的
URIError: URI malformed
at decodeURIComponent( < anonymous > )
at Url.parse(url.js: 269: 19)
at Object.urlParse[as parse](url.js: 75: 5)
at Object.validateUrl[as validate](C: \node\ node_modules\ npm\ node_modules\ n opt\ lib\ nopt.js: 172: 13)
at validate(C: \node\ node_modules\ npm\ node_modules\ nopt\ lib\ nopt.js: 221: 24)
at validate(C: \node\ node_modules\ npm\ node_modules\ nopt\ lib\ nopt.js: 187: 11)
at C: \node\ node_modules\ npm\ node_modules\ nopt\ lib\ nopt.js: 104: 12
at Array.map(native)
at C: \node\ node_modules\ npm\ node_modules\ nopt\ lib\ nopt.js: 70: 15
at Array.forEach(native)
C: \node\ node_modules\ npm\ lib\ npm.js: 45
throw new Error('npm.load() required') ^
在帮手
中<% @user.collections.each do |collection| %>
<h1 class="impact"> <%= collection.name %><br></h1>
<%= collection.stories.count %>
<% end %>
我收到错误消息
<% @user.collections.each do |collection| %>
<h1 class="impact"> <%= collection.name %><br></h1>
<%= number_of_stories_in_collection %>
<% end %>
感谢任何帮助,谢谢!
答案 0 :(得分:4)
'collection'变量不是实例变量,因此帮助程序无法看到它。
将您的观点更改为:
<% @user.collections.each do |collection| %>
<h1 class="impact"> <%= collection.name %><br></h1>
<%= number_of_stories_in(collection) %>
<% end %>
你的助手方法:
def number_of_stories_in(collection)
collection.stories.count
end
这样您就可以正确地将变量传递给帮助程序。
答案 1 :(得分:1)
扩展@ Richard的答案和一点点优化以避免n + 1个查询..
<% @user.collections.includes(:stories).each do |collection| %>
<h1 class="impact"> <%= collection.name %><br></h1>
<%= render_stories_count(collection) %>
<% end %>
助手:
module CollectionsHelper
def number_of_stories_in(collection)
collection.stories.length
end
def render_stories_count(collection)
if (count = number_of_stories_in(collection)).zero?
'No stories in this collection yet'
else
"#{count} #{'story'.pluralize(count)}"
end
end
end