导轨将整数从视图传递到控制器

发布于 2025-01-30 21:14:57 字数 975 浏览 3 评论 0原文

我必须添加一个视图,用户将输入一个数字,我们将根据该数字做出响应。在这里,我询问用户的数字,如果他提交它,我们将为此生成数量的段落。

获取错误未定义的方法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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2025-02-06 21:14:57

栏杆轨道上有很多。没有文件名 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 calling render 'showData', this must also be changed to render 'show_data'. That call may be happening indirectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文