Redirect or display partial?

I have a user who can have 0 for many.

I want to show:

  • a new business page if the user has not created any enterprises or
  • business show page if they have 1 business or
  • business index page if more than one business exists.

Where should I put this logic? in the view or in the controller?

for example, in the controller:

if !@user.businesses.any?
redirect_to new_user_business_path(@user)
elsif @user.businesses.count == 1
redirect_to business_path(@user.businesses.first)
elsif @user.businesses.count > 1
redirect_to businesses_path(@user)
end

or in the view:

<% if !@user.businesses.any? %>
<%= render partial 'no_businesses' %>

etc.

It doesn't look like in the view, but I thought it would be nice to ask for the best practice.

+3
source share
3 answers

, . , , , .

, , , .. else else, , :

<% if !@user.businesses.any? %>
<%= render partial 'no_businesses' %>
<% end %>

else, , contoller .

.

+2

, , @ URL- , (, , )

http://www.application.com/home

(, , ), .

URL-, , .

for user with no business: http://www.application.com/businesses/new
for user with 1 business: http://www.application.com/**business_name**
for user with many businesses: http://www.application.com/user/1/businesses
+1

I think this set is for you,

businessadds a before filter to the controller and puts your logic,

before_filter :check_status

private
    def check_status
        if @user.businesses.blank?
          redirect_to new_user_business_path(@user)
        elsif @user.businesses.count == 1
          redirect_to business_path(@user.businesses.first)              
        end
    end
+1
source

All Articles