Reputation: 8563
Is there a better/cleanier way to write the following in rails:
@next_user = @users[@users.index(@user) + 1]
if (@next_user)
button_to "Next User", click_url(:id => @next_user.id);
else
button_to "Next User", {}, :disabled => true;
end
I have tried
@next_user = @users[@users.index(@user) + 1]
button_to "Next User", click_url(:id => @next_user.id), :disabled => !@next_user;
But the application throws an exception when reaching the last element of the array.
I just want to know if there is a better way to achieve the same behaviour.
Upvotes: 1
Views: 388
Reputation: 7809
Not sure it's much simpler but this should take care of the exception.
@next_user = @users[@users.index(@user) + 1]
button_to "Next User", @next_user.nil? ? {} : click_url(:id => @next_user.id), :disabled => !@next_user;
Upvotes: 2