Ransack: Not able to use link_to

Created on 28 Oct 2013  Â·  5Comments  Â·  Source: activerecord-hackery/ransack

Hello everyone!
I have some hard times using ransack. Let's say i have a basic blog with month archives in the sidebar. Each month is a link to search results based on Ransack.

link_to code looks like this:

= link_to 'January', posts_path(resource, utf8: 1, created_at_gteq: '2013-01-01', created_at_lteq: '2013-01-30')

The problem is, Ransack does not recognizes those params when i'm sending them 'manually'. Everything works fine when i'm using the form helper but when i'll try to fire the exact same query by creating this custom link, ransack does not recognizes my params as search params.

As far as i know, it can be caused by some chars escaping in URLs generated by Ransack form/sort helpers. Here is the URL comparison:

# URL generated by a form
/posts/?utf8=✓&q%5Bcreated_at_gteq%5D=2013-01-01

# URL generated by a link_to
/posts/?utf8=1&created_at_gteq=2013-01-01

And it's probably that cause when i'll rewrite my link_to generated url to use all those escaped chars, it works. Is there any wy to make ransack work a bit better with link_to?

Thanks in advance

Most helpful comment

I assume you're using params[:q] for ransack in your controller. This is evident in the first URL, generated by the search form, where the search parameter is q[created_at_gteq]. In the second, manually generated URL, you're omitting the q namespace, so they aren't even getting passed to ransack. Use this instead:

= link_to 'January', posts_path(resource, :'q[created_at_gteq]' => '2013-01-01', :'q[created_at_lteq]' => '2013-01-30')

All 5 comments

I assume you're using params[:q] for ransack in your controller. This is evident in the first URL, generated by the search form, where the search parameter is q[created_at_gteq]. In the second, manually generated URL, you're omitting the q namespace, so they aren't even getting passed to ransack. Use this instead:

= link_to 'January', posts_path(resource, :'q[created_at_gteq]' => '2013-01-01', :'q[created_at_lteq]' => '2013-01-30')

Oh my, i missed the q parameter, sorry for this! Thanks for your answer and
for this awesome gem! :)

On 28 October 2013 15:06, Will Gray [email protected] wrote:

I assum you're using params[:q] for ransack in your controller. This is
evident in the first URL, generated by the search form, where the search
parameter is q[created_at_gteq]. In the second, manually generated URL,
you're omitting the q namespace. Use this instead:

= link_to 'January', posts_path(resource, :'q[created_at_gteq]' => '2013-01-01', :'q[created_at_lteq]' => '2013-01-30')

—
Reply to this email directly or view it on GitHubhttps://github.com/ernie/ransack/issues/295#issuecomment-27213930
.

@graywh thanks :)

@graywh
how to use this link_to for show method?
can i do like this :
= link_to 'January', post_path(post, :'q[created_at_gteq]' => '2013-01-01', :'q[created_at_lteq]' => '2013-01-30')
i would put this link_to in helper

@nadnidnuds If you need search parameters for ransack, that's exactly how you pass them to a named route helper.

Was this page helpful?
0 / 5 - 0 ratings