Martin Petrov
Martin Petrov

Reputation: 2643

link_to passes arguments to checkboxes

I successfully learnt how to use has_many :through with checkboxes from this blog.

Now I want to have some of these checkboxes checked, based on arguments passed with link_to.

I tried many different things like...

<%= link_to 'New post', new_post_path, :category_ids => [1, 3] %>

Upvotes: 0

Views: 873

Answers (1)

Bjorn
Bjorn

Reputation: 5362

<%= link_to 'New post', new_post_path(:category_ids => [1, 3]) %>

Something like that is a good start, now in your template, where you're adding the checkboxes, just add a small if that checks if there's a category_ids key in params and you should be good to go: (params[:category_ids] || {}).include?(1) where 1 is the number you want to check.

Upvotes: 1

Related Questions