Hi All,
I’m writing a web app for a non-profit NGO that helps farmers in a co-
op plan their crops better to reduce shortages, surpluses and
unhealthy competition between themselves. Right now, I’m doing
something that doesn’t feel Rails-ee and want some feedback. Here’s
the problem.
Here’s the set up: the farmers are Users with a ‘farmer’ role. I want
to all the farmer to view their account to view their User profile
(name, phone, email, etc.) as well as their Sales, Purchases, and the
Crops they are currently managing. (Sales and Purchases are actually
both Orders handled polymorphically, which might not be important for
this discussion).
In the view for User#show I have partial that displays some links that
go back to the Show action but have set a param that gives context to
which chunk of information the person wants to see, such as:
- <%= link_to 'Profile', user_path(current_user, :ui_selector => 'profile') %>
- <%= link_to 'Sales', user_path(current_user, :ui_selector => 'seller') %>
- <%= link_to 'Purchases', user_path(current_user, :ui_selector => 'buyer') %>
- <%= link_to 'Current Crops', user_path(current_user, :ui_selector => 'crops') %>
You can probably tell where this question is going… Inside the
controller, User#show I set instance variables based on the value of
params[:ui_selector], like thus:
@crop = Crop.new(:user_id => @user.id) if params[:ui_selector] ==
‘crops’
@order = Order.new(:buyer_id => @user.id) if params[:ui_selector] ==
‘buyer’
@order = Order.new(:seller_id => @user.id) if params[:ui_selector] ==
‘seller’
Then, also in the view User#show, I show the right stuff based on
params[:ui_selector], like this:
<% if params[:ui_selector] == ‘profile’ %>
Name: <%= @user.name %>
Email: <%= @user.email %>
Phone: <%= number_to_phone @user.cell %>
<% elsif params[:ui_selector] == ‘seller’ %>
<%= render :partial => “sales”, :object => @user %>
<% elsif params[:ui_selector] == ‘buyer’ %>
<%= render :partial => “purchases”, :object => @user %>
<% else params[:ui_selector] == ‘crops’ %>
<%= render :partial => “crops”, :object => @user %>
<% end %>
The sales, purchases and crops partials set up a table and then
basically call partials for those classes indexes using @user.sales,
@user.purchases and @user.crops respectively.
It is all working okay, but alarms go off every time I use logic
blocks in Rails, like there has got to be a better way, I’m missing
something obvious. It doesn’t feel very OOP nor DRY but I can’t see
what I’m missing.
Also, is it bad practice to have the views use the collections of an
instance variable as I have done here with the partials accessing
@user.sales, for instance? It seems like I should be setting maybe a
@sales in the controller, or is the tax on the database the same?
(Come to think of it, I’ll probably need to move that into the
controller if I want to use will_paginate with these.)
Thanks,
Dee
P.S. After we get this rolled out in about two-months time, I’ll throw
it onto github and make it open-source.