我正在按照此处的教程进行操作.这很简单,我按照说明完全按照步骤6.7进行操作.此时,我收到错误
undefined method `each' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
当我尝试访问rails服务器上的index.html.erb时.
我知道数据库工作正常,因为我可以完成步骤6.3中提到的所有内容,创建新帖子并显示/编辑/销毁它们绝对没有问题.
具体来说,问题在于线
<% @posts.each do |post| %>
Run Code Online (Sandbox Code Playgroud)
它基本上声称@posts是零.
我很欣赏这个ROR新手的任何帮助!谢谢.
index.html.erb
<h1>Hello, Rails!</h1>
<table>
<tr>
<th>Name</th>
<th>Title</th>
<th>Content</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
:method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to "My …Run Code Online (Sandbox Code Playgroud) 我有一个登录表单/login,将登录信息发布到/checklogin.如果凭据不正确,我想将用户重定向到/login"错误用户名或密码"的错误消息.但是,我似乎无法弄清楚如何通过一个选项,res.redirect就像你使用res.render.
这样做的最佳方法是什么?我之前看过一个问题,但是在Express的更高版本中它似乎没有用.
我有一个表单字段,该表单字段以“ 05/14/2013-05/22/2013”格式发布日期范围。我的模型有两个独立的日期字段,begin并end从表单字段各自的开始日期和结束日期。将日期范围输入正确字段的最佳MVC方法是什么?
我一直试图在控制器的create方法中手动解构日期范围,但是在创建记录之前,看起来更新后的参数在模型中无法正确显示。
编辑:日期范围将以这种格式显示,因为我使用的是Keith Wood的datepick,它在单个输入字段中输出日期。
我目前正在尝试做的是这个(contract是我的模型名称,dates也是输入日期范围:
beginDate = params[:dates].split("-")[0].strip()
endDate = params[:dates].split("-")[1].strip()
params.delete :dates
params[:contract][:begin] = Date.strptime(beginDate, '%m/%d/%Y')
params[:contract][:end] = Date.strptime(endDate, '%m/%d/%Y')
@contract = Contract.new(params[:contract])
...但是在创建和验证记录时,这些对参数的更改不会显示出来。
我有两个Javascript函数,试图使用Google Maps API Geocode查找地址的纬度和经度:
function getLatLon(address) {
var location = -1;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var lat = results[0].geometry.location.lat();
var lon = results[0].geometry.location.lng();
location = new Array(lat, lon);
//document.getElementById('results').innerHTML = location[0];
} else {
alert("Geocode was not successful.");
}
});
return location;
}
function search() {
var address = document.getElementById('address').value;
var location = getLatLon(address);
document.getElementById('results').innerHTML = location[0];
}
Run Code Online (Sandbox Code Playgroud)
虽然getLatLon()中的location [0]在#results div中打印出正确的数字,但search()中的location [0]返回undefined.你有什么想法可能会发生这种情况吗?我试过从getLatLon()返回一个普通的字符串("Hello"),而且工作得很好.