Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Wrong number of arguments(given 1, expected 0)

I have created checkboxes and I want whenever I check the checkbox a method/action should be called but it's not happing I guess the action remains unfazed and I am getting error.

Wrong number of arguments (given 1, expected 0).

What I did is! created a checkbox in show.html.erb

<%= @files.each do |key,value|%>
  <%=check_box_tag(:key, key,Repository.process)%>
  <%=label_tag(:key)%>
<%end%>

then created a new method named as a process in Repository controller

 def process --> error in this line
      end

and given a route

post 'repositories/:id/process', to "repositories#process"
like image 602
Amitoj Singh Avatar asked Mar 02 '17 06:03

Amitoj Singh


2 Answers

You are defining an action method in controller with name process but process is an already defined action method in rails base controller.

You can override it by providing proper parameters but you cannot create another action method with same name of that.

http://apidock.com/rails/AbstractController/Base/process

You can achieve the functionality that you want by changing the things like this.

1- Change the name of your action in your Repository Controller. Lets name it generate so add below function in your controller. (Please remove process )

def generate
  // Write your custom code to process the params[:id]
end

2- Add this in your routes.rb file

post 'repositories/:id/generate' => "repositories#generate", as: 'generate_repository'

3- Replace your checkbox code from

<%=check_box_tag(:key, key,Repository.process)%>

with this

<%= check_box_tag(:param_name, param_value, false, data: {remote: true, method: :post, url: generate_repository_path(id_you_want_to_send) }) %>

Thats it! When you will check the checkbox you will receive param_value in the param_name in the params hash in your action method. You can access it with params[:param_name].

Whenever you will receive any value in params[:param_name] this means checkbox was being checked if value is nil that means checkbox was unchecked

Please change param_name, param_value and id_you_want_to_send with your desired variables or values.

Thanks!

like image 113
Azmat Rana Avatar answered Nov 04 '22 15:11

Azmat Rana


Your process method accepts no argument but you are sending the checkbox data to it.

The syntax for the check box tag is like this:

check_box_tag(name, value = "1", checked = false, options = {})

You check box tag looks like this:

check_box_tag :key, key, Repository.process 

You are passing an argument to checked which you shouldn't Maybe you would like to use just check_box:

check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")

Also, to trigger a method call when they click the check box, you have to add a remote: true option and send it via Ajax.

I suggest instead of an actual checkbox using a link as it is easier and cleaner. Hope this helps.

like image 1
Alexander Luna Avatar answered Nov 04 '22 15:11

Alexander Luna