导轨将整数从视图传递到控制器
我必须添加一个视图,用户将输入一个数字,我们将根据该数字做出响应。在这里,我询问用户的数字,如果他提交它,我们将为此生成数量的段落。
获取错误未定义的方法nil:nilclass in _show_data.html.erb文件
application Contraption
class ApplicationController < ActionController::Base
private
def baconipsum
@baconipsum ||= Faraday.new("https://baconipsum.com/") do |f|
f.response :json
end
end
end
Contrancter contranter.rb
def showData
@value = params[:value]
@bacon = baconipsum.get("api/", type: 'all-meat',paras: @value).body
end
_form_data.html.erb
<%= form_for:article do %>
<label for="value">Value</label>
<%= text_field_tag :value %>
<%= submit_tag "Submit" %>
<% end %>
_show_show_data.data.html.erb.html.erb
<% @bacon.each do |meat| %>
<p><%= meat %></p>
<%end%>
I have to add a view where user will enter a number and we will respond according to that number. Here I am asking a number from user and if he submit it we will generate number of paragraph for that.
Getting error undefined method `each' for nil:NilClass in _show_data.html.erb file
Application Controller
class ApplicationController < ActionController::Base
private
def baconipsum
@baconipsum ||= Faraday.new("https://baconipsum.com/") do |f|
f.response :json
end
end
end
Articles Controller.rb
def showData
@value = params[:value]
@bacon = baconipsum.get("api/", type: 'all-meat',paras: @value).body
end
_form_data.html.erb
<%= form_for:article do %>
<label for="value">Value</label>
<%= text_field_tag :value %>
<%= submit_tag "Submit" %>
<% end %>
_show_data.html.erb
<% @bacon.each do |meat| %>
<p><%= meat %></p>
<%end%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
栏杆轨道上有很多。没有文件名 ever 包含大写字母,
showdata
之类的文件名应该永远存在。这不是意见的情况,这是Rails从模型名称中正确推断文件名所必需的。您的文件名称应为
_form_data.html.erb
和_SHOW_DATA.HTML.ERB
。无论您在哪里调用渲染'showdata'
,也必须将其更改为渲染'show_data'
。该电话可能是间接发生的。Casing maters a lot in Rails. No filename should ever contain a capital letter, and filenames like
showData
should never exist. This isn't a case of opinion, this is required for Rails to correctly infer the names of files from the names of models.Your filename names should be
_form_data.html.erb
and_show_data.html.erb
. Wherever you're callingrender 'showData'
, this must also be changed torender 'show_data'
. That call may be happening indirectly.