6

I don't understand what's happening here. I want to put a button_to in my (haml) view. If I do this:

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

the page generated 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 OK, but I need to address a different controller. But if I try to specify the 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 to be "/editor/new", and I have no idea why it isn't, nor how to correctly specify the controller I want to route to.

I'm using Rails 3.2.1.

Ian Dickinson
  • 12,875
  • 11
  • 40
  • 67

2 Answers2

7

Try

=button_to( "New", new_editor_path, :method => :get )
Syed Aslam
  • 8,707
  • 5
  • 40
  • 54
7

You don't want to include the options in their own hash, I think this is confusing the interpreter.

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

should do what you want.

TheDelChop
  • 7,938
  • 4
  • 48
  • 70
  • Hmm, OK that works. Thanks! I'm confused though, because I thought I was following the documentation which shows `button_to` with three params: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to – Ian Dickinson Feb 29 '12 at 15:21
  • Yeah, I don't know why the documentation for link_to and button_to are different this respect. Just upvote/accept my answer, please! Thanks! – TheDelChop Feb 29 '12 at 15:25
  • "Attribute 'controller' not allowed on element input at this point" is the HTML5 validation error this will create, as your input will be something like `` rather than it simply containing `type="submit"` and perhaps Value and Class etc, relying on the path supplied by the parent form tag. The other answer will give `
    `
    – xxjjnn Jul 30 '14 at 10:25