1

Controller:

def store
  if params[:name].present? && params[:pass].present? && params[:cpass].present? && params[:role].present?
    @name = params[:name].downcase

    if CheckEmpName(@name) == 0     
      render "userexist"
      return
    end

    pass = params[:pass]
    cpass = params[:cpass]

    if pass != cpass                
      render "passerror"
      return
    end

    role = params[:role]

    @user = Employee.new(name: @name, password: pass, role: role)
    @user.save

  end
  render "success"
end

The above action renders the "success.html.erb" page when all the user inputs are valid. Instead if the username is already registered or both the password and confirm password are not matched then it renders the appropriate error files which is "userexist.html.erb" or "passerror.html.erb".

Problem:

I am able to access @name instance variable in success.html.erb file. But I am not able to access that in both of the error html files. Why it is only accessible in "success" and not in "userexist" and "passerror". Why it is not accessible. And What I need to do to access those variable in both of the error handling html pages ?

7
  • 1
    Show your views (working and non-working) Commented Sep 14, 2016 at 8:46
  • You shuld be using render 3 times, you should render only one and pass flash[:error] to them, or use partials. Even rails shows an error when you use render or redirect many times in a single method. Commented Sep 14, 2016 at 8:53
  • "I am not able to access that in both of the error html files" Where is this happening? I don't see where userexist or passerror are being rendered. Commented Sep 14, 2016 at 8:58
  • 1
    @SergioTulentsev. Thanks. I done my mistakes in my views. Instead of specifying "@name", I specified "@user". That is the mistake. Commented Sep 14, 2016 at 9:02
  • @JorgedelosSantos: this code will not produce "double render" error. Commented Sep 14, 2016 at 9:06

1 Answer 1

1

You may explicitly pass whatever you want to access as locals hash parameter to call to render:

render "userexist", locals: { name: @name }

Now name will be passed through to underlying template.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to send local variable instead of instance variable ?
Sure, the hash will be passed through as is, think about it as about plain ruby. One might put there whatever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.