简体   繁体   English

Rails将整数值从视图传递到控制器

[英]Rails passing integer value from view to controller

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在_show_data.html.erb文件中为nil:NilClass获取错误未定义方法`each'

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文章控制器.rb

def showData
    @value = params[:value] 
@bacon = baconipsum.get("api/", type: 'all-meat',paras: @value).body
  end

_form_data.html.erb _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 _show_data.html.erb

    <% @bacon.each do |meat| %>
  <p><%= meat %></p>
<%end%>

Casing maters a lot in Rails.套管在 Rails 中重要。 No filename should ever contain a capital letter, and filenames like showData should never exist.任何文件名都不应该包含大写字母,并且像showData这样的文件名不应该存在。 This isn't a case of opinion, this is required for Rails to correctly infer the names of files from the names of models.这不是意见,这是 Rails 从模型名称正确推断文件名称所必需的。

Your filename names should be _form_data.html.erb and _show_data.html.erb .您的文件名应该是_form_data.html.erb_show_data.html.erb Wherever you're calling render 'showData' , this must also be changed to render 'show_data' .无论您在哪里调用render 'showData' ,都必须将其更改为render 'show_data' That call may be happening indirectly.这个电话可能是间接发生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM