0

I vahe view form, this form have:

<%= link_to day, root_path(:day => day), :id => 'link', :class => 'active_link' %>

This link reneder this form with day parameter and items on this form rendered with this parameter. I want that after clicking on this link, after re-rendering form - class 'active_link' removed. How can I make it?

P.S: this is link for item (day item from collection).

2 Answers 2

1

This is a view logic problem, not a JavaScript/jQuery problem. You are using a normal link to change the day parameter and re-render the entire page. Based on the only line of code you are showing us, I'm guessing your view loops through a bunch of dates and prints a link out for each one. The problem is, you are assigning the active_link class to all of the day links in your view. "Fixing" this problem with jQuery in the browser after the page loads is the wrong approach; instead fix your view logic to only assign the active_link class to the one correct link (and ditch the non-unique 'link' ids).

In your controller:

@active_day = params[:day]

In your view:

<% days.each do |day| %>
  <%= link_to day, root_path(:day => day), :class => (day == @active_day ? 'active_link' : '') %>
<% end %>

Obviously, you'll need to tweak that code some to work in your app - you haven't shown enough code for me to write a working code solution.

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

Comments

0

You can do:

$('a.active_link').click(function(){
      $(this).removeClass('active_link');
});

I'm no expert of ruby, but this works if your link is rendered as

<a id='link' class='active_link'></a>

3 Comments

is your id on the anchor unique? For a while I made the mistake of having the same id on identical elements...the binding of the click was picking up just one of the things I had with that id. if you want to get just one, use a unique id, if you want to get more than one of the same conceptual thing, use an "id css class"...that is, one with no real css definition backing it. I usually distinguish those from real css classes by appending "-id" at the end.
As jaydel says id must be unique. I try to rewrite my code, try it
Can you post the html as is outputted to the browser?

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.