1

Hello right now I've a problem about button_to

I want to setup a button that call a method in controller, this is how I setup the button :

(this located in issues/_edit.html.erb)

<%= button_to "Cancel Return", :action => "cancel_return", :controller => "issues" %>

and I want this will call this function in issues_controller.rb

  def cancel_return
    @issue.cancel_return(params)
  end

I also added it in the routes.rb

map.issue_cancel_return 'issues/cancel_return', :controller => 'issues',
                       :action => 'cancel_return'

but it can't work, I already tried to declare cancel_return as helper_method, but it also didn't work. is there any other solution? or I'm doing it wrong? I'm using ruby version 1.9.3p125 and rails version 2.3.15, I used old version because I tried to modify redmine, any help will be appreciated.

Thanks

Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
  • is issues#cancel_return a valid route? i haven't seen redmine's routes.rb but did you also the new action in there aside from adding it in the controller? – roninblade Aug 28 '13 at 05:18
  • @roninblade actually I'm not actually sure how to put it in the routes.rb, I added it like this map.issue_cancel_return 'issues/cancel_return', :controller => 'issues', :action => 'cancel_return', you can also see it in the edited question – Niko Adrianus Yuwono Aug 28 '13 at 05:39
  • what happens when you call the action directly on your browser? ie. http://127.0.0.1:3000/issues/cancel_return/?plus_your_extra_params – roninblade Aug 28 '13 at 07:09
  • @roninblade error 403 not auothorized even I'm login to the site as admin – Niko Adrianus Yuwono Aug 28 '13 at 09:02

1 Answers1

2

you should be putting them in a curly braces like this

<%= button_to "Cancel Return", { :controller => "issues", :action => "cancel_return"} %>

what happening is that map is not defining the method for the call like GET/POST/DELETE so what you can do is use this instead of you map line in your routes.rb file

get "issues/cancel_return" => "issues#cancel_return"

this should solve your problem.

dirtydexter
  • 1,063
  • 1
  • 10
  • 17
  • still nothing happens :(, how can I know the function is called beside the database change? is there any method like php print_r("TEXT");exit; in RoR?just to make sure my button works – Niko Adrianus Yuwono Aug 28 '13 at 04:50
  • look at you server log, that what request is being happening and what is called and what not. – dirtydexter Aug 28 '13 at 04:54
  • I guess it's not called the last 3 action showed in the log after I clicked the button is Processing IssuesController#show (for 172.16.20.1 at 2013-08-28 11:42:16) [GET] Parameters: {"controller"=>"issues", "action"=>"show", "id"=>"833"} Rendering template within layouts/base Rendering issues/show Completed in 408ms (View: 65, DB: 316) | 200 OK do you know why it's not called? – Niko Adrianus Yuwono Aug 28 '13 at 04:59
  • @nayoso can you please write the path that you have given in the routes.rb, show what poppes out with both `show` and `cancel_return` methods. – dirtydexter Aug 28 '13 at 05:40
  • i meant do `rake paths` and tell me what method each action is showing you know like 'get/put/delete' types – dirtydexter Aug 28 '13 at 05:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36372/discussion-between-dirtydexter-and-nayoso) – dirtydexter Aug 28 '13 at 05:51