[LRUG] Moving logic from view to controller/model

Kevin Fullerton kevin.fullerton at shotgun-suicide.co.uk
Fri Jul 25 10:14:33 PDT 2008


Hi,

 

I've got a little pet project I'm working on with RoR and I've added some
logic to the view that I know should be in either the controller or the
model.

 

The offending code is

 

<p>Roles<br/>

<%= hidden_field_tag "user[role_ids][]", "" %>

<% for role in Role.find(:all) %>

<% if @user.login == 'admin' && role.role_name == 'System Administrator'  %>

  <%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role),
:disabled => true %>

  <%= hidden_field_tag "user[role_ids][]", role.id %>

<% else  %>

  <%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role)
%>

<% end  %>

<%= role.role_name %>

<% end %>

</p>

 

Basically, Role is a habtm join to User - I want to show all available
Roles, clearing the habtm association if none are selected, apart from where
the user is admin and the Role is System Administrator - that must always be
selected (to ensure that no db monkeying is required when the inevitable
happens and admin is removed from the System Administrator role)

 

So - should this go in the model in a before_save callback or in the
controller - my gut says model but wanted to call on the wealth of knowledge
here J

 

Many thanks

 

Kevin Fullerton

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20080725/db2f8a0c/attachment-0003.html>


More information about the Chat mailing list