views:

99

answers:

1

I have a method defined in my controller that I am trying to create a button or form to access.

Model

class DoThis < ActiveRecord::Base

 def take_action(a, b)

 end

end

View

<%= @do_this.take_action(@a, @b) %> 

I would like to convert the above code in the View to a button_to or form_for but cannot figure out how.

Thanks

+1  A: 

You shouldn't do this. Models should never contain view code - it's intentionally not easy to do that, because it breaks MVC. Instead, you should add a helper function in your app/helpers/controller_name_helper.rb file.

def take_action(obj)
  button_to( ... )
end

Then, you'll just call <%=take_action(@do_this) %> in your view.

Chris Heald
Would I put the additional code that is currently defined in the "take_action" method in the Model into the new "take_action" method in the controller helper?
Joey
I defined the action in the controller helper instead of the model and now I am running into another problem. The code I had inside of "take_action" was to update.attributes on another model--DoThisTwo. The code worked properly when it was defined in the original DoThis model but now I am getting a NoMethodError with "undefined mehtod 'attribute' when it is in the CONTROLLER of DoThis. I added attr_accessible for the attribute I am updating in DoThisTwo. Any other suggestions?
Joey
got it working thx
Joey