Cameron Bass Cameron Bass - 5 months ago 10
Ruby Question

Controller method not redirecting properly

I currently Have a controller method that increments 1 on a attribute and then it should redirect to a particular page with that user found. But for a reason I can't figure out it keeps rendering the else statement and the end of the method. I'll post my code then clear things up once you see it.


def search
@subscriber =

def visit
@subscriber = Subscriber.find_by_phone_number(params[:phone_number])
if @subscriber
@subscriber.visit ||= 0
@subscriber.visit += 1
flash[:notice] = "Thank You #{@subscriber.first_name}. You have #{@subscriber.days_till_expired} until renewal"
redirect_to subscribers_visit_path(:subscriber)
render "search"


<h1>Hello Subscriber</h1>

<% if @subscriber %>
<td><%= image_tag avatar_url(@subscriber) %></td>
<td><%= @subscriber.first_name %></td>
<% else %>
<tr><td>No subscriber found!</td></tr>
<% end %>

For whatever reason it keeps rendering the "search" in the else part of the method and not the subscriber_visit_path but it does the
+= 1
part just fine. So, basically it's finding the subscriber but after it increments it seems to make the subscriber go nil? Am I missing something obvious here?


I agree with the comment from Mr Bass - do you have tests to make sure the process is working?

Especially the if @subscriber.

I might try using the built in exists? in your if statement, this guarantees a true or false outcome.