发布表单详细信息时,我有一个基本的Sinatra应用程序。我收到此错误,尤其是在Heroku上运行时
NameError - 未定义的局部变量或方法`user_details'对于#:
Mye post方法:
post "/" do
user_details = UserDetails.new(params["name"], params["email"])
if user_details.valid?
begin
worksheet.insert_rows(worksheet.num_rows+1, [user_details.to_row])
worksheet.save
erb :thanks
rescue
erb :index
end
else
erb :index
end
end
查看:
<form action="/" method="POST">
<div class="form-group<%= 'has-error' if user_details.errors.include?(:name) %>">
<label for="name" class="control-label">Name</label>
<input type="text" class="form-control" name="name" id="name" value="<%= user_details.name %>">
<% user_details.errors.full_messages_for(:name).each do |message| %>
<span class="help-block"><%= message %></span>
<% end %>
</div>
</form.
日志说它无法在第一个div块/类中找到user_details
变量。
有什么想法吗?
thanks.erb
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Thanks!</h1>
<p>We'll let you know when our platform is ready. Stay tuned!</p>
</div>
<div class="col-md-4">
<a href="/"><button type="submit" class="btn btn-primary">Back</button></a>
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
您为user_details
定义了一个局部变量,该变量在视图中不可用。两个选项:
@user_details
并在视图中使用它。user_details
(就像现在一样)并渲染视图,传递变量:erb :thanks, locals: {user_details: user_details}
(或erb :index, locals: {user_details: user_details}
,不确定错误来自何处)