Home>

If i mistakenly enter a number in full-width in the url as shown below, flash [: danger] = "Please make id half-width"
How can I get a message?

http: // localhost: 3000/users/7/edit

↓ It is the controller of the corresponding part
* It is set as before_action on edit and show pages.

def set_user
    @user = User.find (params [: id])
    if @ user.id == @ user.id.to_s
      flash [: danger] = "Please make id half-width"
    end
 end

* Currently, such an error is appearing.

  • Answer # 1

    In the current code, DB search is performed with the ID that is full-width, so an error has occurred because User data could not be acquired.

    So, before that, convert the specified ID (params [: id]) to half-width and use the half-width ID to perform User.find ().

    However, even in that case, an ActiveRecord :: RecordNotFound exception may occur, so take corrective action.

    Only when an exception does not occur, if the specified ID and the acquired User id do not match, an error will be notified.

    We're sorry, but please create the code yourself.