0

I have a situation where I need to do an ajax call and post data to the controller. Since it is a custom submission, the normal params are not accessible to me in the controller. Therefore when I want to save the data I don't have the ids that I want.

my url is like this localhost/events/2/roster/1

I need 2 and 1 in my controller so I can manipulate those records. I considered doing something like a hidden input field on the page with

<%= @event.id %>
<%= @roster.id %>

Then pulling the value into coffeescript but it is making me cringe to do it.

Is there a better way to access this data in the controller?

Can you pass the normal params through ajax to the controller?

thank you for the input.

EDIT Normally when you do a form submit like form_for you get back params which has the data for you including the id of the object. When I do my ajax POST, the id fields are not available to me.

By custom submission I mean its an AJAX post versus a form submit.

EDIT 2 Routes:

resources :events do
  resources :rosters
end

post 'rosters/validate'

thanks,

3
  • It's a bit unclear what you are referring to with the terms "custom submission" and "normal params". Could you elaborate? Commented Jun 15, 2017 at 12:48
  • I still don't quite understand how come "the id fields are not available". Is the URL you provided the endpoint you are doing your ajax post against? If so, surely you have the fields available for your controller. Is it a question of how to access the parameters? Commented Jun 15, 2017 at 13:12
  • I think so? Is there an easy way to get the event id 2 and the roster id 1 in the controller? I am doing the ajax request to a function called validate in my controller which is where I would need those values. Commented Jun 15, 2017 at 13:13

1 Answer 1

1

Given that your route/controller has no information of the IDs, yes, you will need to pass the IDs via the POST request.

Hidden input fields with the desired IDs is one way to go, and another could be embedding a <script> tag where you directly inject the values as JavaScript. I.e. something like

<script>
  window.event_id = <%= @event.id %>;
  window.roster_id = <%= @roster.id %>;
</script>

And then you add them to your POST payload. If you already have a form you're submitting via AJAX, it makes sense to use hidden input fields.

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

5 Comments

when I watch log/development.log on the ajax submission I do not see those params passed in.
@jacksonecac could you show the route specification and the controller method that are being called?
I added the routes.
I didn't realize you could do something like this. That is cool. I give it a shot tonight and update the answer. Thanks!
@jacksonecac If the answer worked out for you, please consider accepting 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.