开发者

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜