0

I have a controller method for login my app like this:

def create
  user = User.find_by(mail: params[:session][:mail].downcase)
  if user && user.authenticate(params[:session][:password])
    if user.confirmed? 
        # Stuff when login is OK
        .......         
    else
        logout
        text = I18n.t("error.login.confirmation", :link => ActionController::Base.helpers.link_to(I18n.t("button"), confirm_user_path(user), :class => 'btn btn-info'))
        @result = ActionController::Base.helpers.sanitize(text, :tags => ['br','a']).html_safe
        respond_to do |format|
            format.html { render 'new' }
            format.js { @result.html_safe }

        end
    end
  # more stuff
  .......
  end  
end

If the user has not confirmed the email, I want to show him a message with a link to re-send the confirmation mail:

 Please confirm your signup. <br /> <br /> <a class="btn btn-info" href="/users/48/confirm_user">Re-send confirmation mail.</a>

UPDATE: This is how render the view

$("p.bg-danger").html("<%= @result %>")
2
  • I'd escape them with html entities, such as &gt; and &nbsp; then parse those client side and add to dom with js or jquery. Commented Nov 14, 2014 at 21:17
  • 1
    @Todd: I have updated the question with the render view. How do you do that parsing? Commented Nov 14, 2014 at 21:32

1 Answer 1

3

In create action, it is not needed to type @result.html_safe, because it's actually does nothing here. Instead, you would like probably to tell your controller not to render layout.

def create
  # ...
  respond_to do |format|
    format.html { render 'new' }
    format.js { render layout: false }

  end
end

Then, within create.js.erb:

$("p.bg-danger").html('<%=j @result %>');
Sign up to request clarification or add additional context in comments.

2 Comments

It works! But I don´t why. How @result reachs the view? By the '@' in the controller?
Yep, @ means that given variable becomes instance variable. Instance variables are available in any instance controller methods. So that, any instance variable defined in controller is also available within view, because it just a template (ERB in this case) rendered by appropriate controller method.

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.