Home > database >  Rails passing integer value from view to controller
Rails passing integer value from view to controller

Time:05-23

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%>

CodePudding user response:

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.

  • Related