Rails: button to
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.
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;
精彩评论