Chris Williams
Chris Williams

Reputation: 432

Accessing params from ajax modal

In my rails app I have an index controller method which has the bucket_id stored in the url params like this: http://localhost:3000/posts?bucket_id=3 When I call params it returns {"bucket_id"=>"3", "action"=>"index", "controller"=>"posts"}

On this page, I also have a button that opens an ajax modal to the new path of the same controller. When I call params here, it returns {"action"=>"new", "controller"=>"posts"} and I'm not able to access the bucket_id. Although the browser URL still says http://localhost:3000/posts?bucket_id=3

I can't seem to figure out how to pass the params from the index method to the new method when I click to open the ajax modal.

Any ideas? Thanks.

Upvotes: 0

Views: 53

Answers (1)

Chris Williams
Chris Williams

Reputation: 432

@Abhi 's solution worked for me as I was using the rails link_to helper:

<%= link_to '', new_post_path(:bucket_id => params[:bucket_id]), remote: true, id: 'js-topbar-add' %>

and I was able to access the bucket_id in the modal by simply calling params[:bucket_id]

Thanks!

Upvotes: 2

Related Questions