ArgumentError in RequestsController#create wrong number of arguments (given 1, expected 2..3)

Beginner in rails here, so please stick with me.

In my simple application I am trying to create a form with a text field. After the user enters some text, that text will be used for further processing.

I have created a request model for storing that text and I am using the standard way of fetching the parameters in my create method once the user clicks the submit button

My files are:

app/controllers/requests_controller.rb

class RequestsController < ApplicationController
  include HTTParty

  def create
    @request = Request.new(request_params)
    if @request.save
    HTTParty.options(http_proxyaddr="my_proxy")
    @res = HTTParty.get("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=#{@request.text}&api_key=my_api_key&format=json")
    render 'show'
    else
      # nothing yet
    end
  end

  private
    def request_params
      params.require(:request).permit(:text)
    end
end

app/models/request.rb

class Request < ApplicationRecord
  validates :text, presence: true,
                    length: { maximum: 100 }
end

/db/migrate/create_requests.rb

class CreateRequests < ActiveRecord::Migration[5.1]
  def change
    create_table :requests do |t|
      t.text :text

      t.timestamps
    end
  end
end

app/views/sessions/home.html.erb

<h1> welcome! <h1>
<% if logged_in? %>
    <%= form_for(:request, url: request_path) do |f| %>
      <%= f.label :text %>
      <%= f.text_field :text, class: 'form-control' %>
      <%= f.submit "search", class: "btn btn-primary" %>
    <% end %>
    <%= link_to "Log out", logout_path, method: :delete %>
<% else %>
    <ul>
    <li><%= link_to "log in", login_path %></li>
    <li><%= link_to "sign up", signup_path %> </li>
    </ul>
<% end %>

config/routes.rb

Rails.application.routes.draw do
  get 'sessions/new'

  root 'sessions#home'
  get  '/signup',  to: 'users#new'
  post '/signup',  to: 'users#create'
  get   '/login',   to: 'sessions#new'
  post   '/login',   to: 'sessions#create'
  delete '/logout',  to: 'sessions#destroy'
  post '/request', to: 'requests#create'
  resources :users
  resources :requests

end

Error I am getting is:

screenshot

{"utf8"=>"✓", "authenticity_token"=>"h3yj54YtfZjBMdP50qsh7w0MEty/DOzBEqoYPyacT3dyoqHNQyPGDTdUGCOPoJ6szBQFVT7cAlpR19+PO0ebgg==", "request"=>{"text"=>"FGFGFG"}, "commit"=>"search"}

will appreciate any help.

1 answer

  • answered 2018-04-17 05:20 Deep

    You are using httparty gem and you have this line in controller:

    include HTTParty
    

    Now if you dig into the gem code (see the below link):

    Request Class

    the gem already has Request class and you have created the same name model in your application. So it would be better to rename your model to something else to avoid confusion.

    Or else you can try this in your create action:

    @request = ::Request.new(request_params)
    

    The :: may solve your issue. But it is better to always use different names for the classes so we don't face any issues in future when the application grows.