0

I tried to use jQuery in a Rails view with if but it didn't work well. Any advicse?

This is the view:

 jquery when the page ready
 :javascript
  $(document).ready(function{
 $('#msg').hide(); 
  })

 -if !@x || !@p 
  :javascript
   $(document).ready(function{
   $('#msg').show(); 
   })
  %h5.alert.alert-error#msg.put Invalid email or password 

This is the controller:

@x=Profolio.find_by_email(params[:user])
@p=Profolio.find_by_password(params[:userpassword])
1
  • It's best if you keep your Javascript in separate files and have them included in rather than code them into the view. Just good practice with "separation of concerns." Commented Apr 5, 2013 at 3:43

1 Answer 1

1

For one, it looks like your jQuery is malformed. Your view should look like:

:javascript
$(document).ready(function()
{
  $('#msg').show();
});

Notice the () by function.

I am also unsure if you're using HAML or what, but :javascript could just be :script.

Also, Rails should load jQuery by itself as jQuery is the defacto JavaScript framework of newer versions of Rails.

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

2 Comments

Your last remark is wrong, regardless of where the Javascript is, if it's dependent on the document is should wait for the document to load before firing.
@izuriel Thanks, I removed it.

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.