14
निम्नलिखित कोड है:गार्ड के बजाय एक सशर्त अभिव्यक्ति के अंदर कोड लपेटकर के खंड रेल
# API controller for authentication
class Api::V1::SessionsController < Api::V1::ApplicationController
skip_before_action :authorize
def create
@user = User.find_by(email: params[:user][:email])
unless @user && @user.authenticate(params[:user][:password])
@error_message = 'Invalid username or password'
render 'shared/error', status: :unauthorized
end
end
end
मैं अगर यह रूबी दिशा निर्देशों के मेल खाता है Rubocop मेरी कोड की जांच करने का उपयोग करें। मैं निम्नलिखित त्रुटि मिली:
Use a guard clause instead of wrapping the code inside a conditional expression.
unless @user && @user.authenticate(params[:user][:password])
तो, मुझे समझ नहीं आता कि कैसे मैं बेहतर गार्ड खंड का उपयोग कर इस कोड को बना सकते हैं। अग्रिम में धन्यवाद!
अच्छा नहीं। यदि उपयोगकर्ता && user. प्रमाणीकरण मुझे डिफ़ॉल्ट – malcoauri
द्वारा दृश्य प्रस्तुत करने की आवश्यकता है जो ऊपर से ओसमैन आपके कोड के बराबर है। जब उपयोगकर्ता सफलतापूर्वक प्रमाणित करता है तो यह अभी भी टेम्पलेट को प्रस्तुत करेगा। – jvnill