Rails button_to: how to specify a controller?

I don’t understand what is going on here. I want to put button_to in my (haml) view. If I do this:

 =button_to( "New", {:action => "new"}, {} ) 

The generated page has:

 <form action="/cached_input_files/new" class="button_to" method="post"> <div> <input type="submit" value="New" /> <input name="authenticity_token" type="hidden" value="..blah.." /> </div> </form> 

which is normal, but I need to contact another controller. But if I try to specify a controller:

 =button_to( "New", {:action => "new", :controller => "editor"}, {} ) 

I get:

 <form action="/assets?action=new&controller=editor" class="button_to" method="post"> <div> <input type="submit" value="New" /> ... 

I expected the action be "/editor/new" , and I have no idea why this is not the case and how to correctly specify the controller to which I want to go.

I am using Rails 3.2.1.

+6
source share
2 answers

You do not want to include parameters in your own hash, I think this confuses the interpreter.

 =button_to( "New", :action => "new", :controller => "editor") 

should do what you want.

+6
source

Try

 =button_to( "New", new_editor_path, :method => :get ) 
+5
source

Source: https://habr.com/ru/post/909637/


All Articles