1

I'm developing a small application in Ruby-On-Rails. I want to call a controller's method from a view. This method will only perform some inserts in the database tables. What's the correct way to do this? I've tried something like this but apparently the method code is not executed:

<%= link_to 'Join', method: join_event %>
notGeek
  • 1,394
  • 5
  • 21
  • 40

2 Answers2

6

The method option in a link_to method call is actually the HTTP method, not the name of the action. It's useful for when passing the HTTP Delete option, since the RESTful routing uses the DELETE method to hit the destroy action.

What you need to do here, is setup a route for your action. Assuming it's called join_event, add the following to your routes.rb:

match '/join_event' => 'controllername#join_event', :as => 'join_event'

Be sure to change controllername to the name of the controller you are using. Then update your view as follows:

<%= link_to 'Join', join_event_path %>

The _path method is generated based on the as value in the routes file.

To organize your code, you might want to encapsulate the inserts into a static model method. So if you have a model called MyModel with a name column, you could do

class MyModel
  # ...
  def self.insert_examples
    MyModel.create(:name => "test")
    MyModel.create(:name => "test2")
    MyModel.create(:name => "test3")
  end
end

Then just execute it in your action via:

MyModel.insert_examples
agmcleod
  • 13,321
  • 13
  • 57
  • 96
  • Thank you so much for your reply. It was very helpful :) How do I do if I want the button to show a form and based on form fields perform the insertions in the database? – notGeek Apr 25 '12 at 18:16
  • If you want to accept user input, it's a matter of a) setting up a model, b) running the database migration for that model and c) setting up a controller & views to support it. I actually suggest that you go through the getting started guide: http://guides.rubyonrails.org/getting_started.html#getting-up-and-running-quickly-with-scaffolding, as it lays out some of these steps. If you prefer to perhaps invest in a book, check out Rails 3 in Action – agmcleod Apr 26 '12 at 02:52
5

In addition to agmcleod's answer, you can expose controller methods to the view with ActionController::Base::helper_method:

class EventsController < ApplicationController
  helper_method :join_event

  def join_event(event)
    # ...
  end
end

But in this case, I think you're best off following his advice and moving this method to the model layer, since it's interacting with the database.

Brandan
  • 14,735
  • 3
  • 56
  • 71
  • You should really not do this. While you can it's more or less ideal to keep the controller know about it's methods, and nothing else :) – agmcleod Apr 26 '12 at 02:49
  • 2
    @agmcleod I disagree. A `logged_in?` controller method that depends on session data is extremely useful in the view layer. As with most things, you just have to know when to use it and when to abuse it :-) – Brandan Apr 26 '12 at 12:53