开发者

Rails: button to

开发者 https://www.devze.com 2023-03-02 12:31 出处:网络
Is there a better/cleanier way to write the following in rails: @next_user = @users[@users.index(@user) + 1]

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

精彩评论

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