Reputation: 253
I would like to show the state from the "skill" controller with this method :
def also_expert?
Patent.where(:user_id => current_user, :skill_id => @skill.id, :state => 1).first
end
So in the view I can use :
<% if also_expert? %>
Expert
<% end %>
When I use it in a partial from an other controller I have a "undefined method" That I understand
<%= render :partial => 'shared/skill', :collection => topic.skills, :locals => { :topic => topic.community } %>
How can I use it in a partial ?
Upvotes: 2
Views: 112
Reputation: 51697
Determining who or what is responsible for a particular piece of logic can be challenging. Refactoring Ruby and Rails AntiPatterns have been a huge help for me in sorting these types of problems out. If you think about the responsibility of the controller and each of the models, it doesn't really make sense that the controller should know if a user is an expert at a skill. If you can convince yourself that this logic belongs in the model, then it's a matter of which model it belongs in.
One might argue that it's the user's responsibility to know this, while another might argue that it's the skill's responsibility. Either way, moving it to one of those models will make your code easier to maintain and understand.
Without knowing too much more about your code, it really comes down to which "feels right". I would probably say that this is the Skill model's responsibility and move the logic there:
class Skill
def user_is_expert?(user)
patents.where(:user_id => user.id, :state => 1).first
end
end
From your view:
@skill.user_is_expert?(current_user)
Upvotes: 1
Reputation: 40313
Define this method in a helper instead of a controller:
module ExpertHelper
def also_expert?
Patent.where(:user_id => current_user, :skill_id => @skill.id, :state => 1).first
end
end
And you will be able to use it on views.
Upvotes: 0