Finding form fields in a Rails POST request - ruby-on-rails

I'm using a plain old HTML form to send a post request to a rails app. Inside the app I would like to grab some of the input fields. I'm having trouble finding them inside the request.
Trying this:
logger.debug "Request Headers #{request.headers.inspect}"
Spits out a massive amount of data but I cannot find my form fields in there. Does anyone know where I can find them?

You can grab the request parameters submitted using:
logger.debug params.inspect
That will show all the form data submitted.

Related

Captcha with Multipart Form Rails

I have a multipart form that I need a captcha for at the end. Essentially, a user is allowed to create/update a draft but not submit it for admin review until everything is done. There is a captcha meant for the last submission but the problem is that when I add it to the form, I can't use any of the other submit buttons because the captcha isn't filled out. Is there any way around this?
I'm using simple_captcha and Rails 3.2.
Thanks!
I haven't used simple_captcha before, but seems like you are doing #object.save_with_captcha in every case. You have multiple options to solve this, but one i came up with is:
In the controller, verify if all the fields (mandatory only i guess) are filled, and if they are, then save your object using #object.save_with_captcha, otherwise do the usual #object.save which wont trigger the captcha validation. Something like this:
def create
#object = MyObject.new(params[:my_object])
if #object.has_mandatory_fields_filled?
#object.save_with_captcha
else
#object.save
end
end
In the has_mandatory_fields_filled? method you would check that all the mandatory fields of your form are not empty/nil etc.

checking the form data received in ruby on rails controller

I want to check the form data received from the form in ruby on rails controller.
I tried this but nothing is shown:
#user.to_yaml
where #user is the form name(<%=form_for #user )
the data sent by a form is stored in the params hash (see this question for more information).
also, in development, just put a raise in your controller action to show an error page with all request parameters displayed.
The above answer is correct. but will like to change something
all the details are stored in params only
and it will be better to show those in flash by doing
flash.notice = params
as it will not effect the working of app
Why don't you check out the server log on the console or use the debugger gem that would keep track of the params and the files along which the request moves along...

Handling forms in rails

I am a little confused with forms in RoR.
I have a contacts page with a corresponding method in my controller. What I am trying to do is have a form so people can leave a message. I will then be emailing that message to myself.
I have the form and everything created. However, I am a little confused on how I would actually get the data from the form once they hit the submit button. would it just be accessible through my contacts method in my controller using params[:message]?
Also, what if I had multiple forms on one page? Would I just be doing params[:message1], params[:message2], etc., in the contacts method in my controller?
Take a look at this answer for details on what exactly the params hash is. When you reference params[:message], this implies that you are POST'ing to your controller action with, say, "message[subject]=abc123", which Rails helpfully turns into a hash with a key like: params[:message]['subject'].
If you're looking to send an email, check out mail_form, which simplifies creating a non-database-backed model that get's turned into an email.
Lastly, about having multiple forms on a page: each form POST's to its action and includes all of the form elements that are children of that form in the DOM. So, only the children of that message will actually be included in the params[:message] hash. You can use fields_for to have multiple models within a single form.

Rails 3 + Tumblr API: How can I edit multiple posts at the same time with one form?

I'm trying to write a Rails app that fetches a user's Tumblr posts that match certain criteria (for example, only posts that contain certain tags). Once the posts are displayed, I want the user to be able to edit them all at once via one form submit button.
I am authenticating to Tumblr via OmniAuth and making API calls using the tumblr_client gem. See the source code here: https://github.com/monfresh/Fix-my-Tumblr-tags
So far, I've been able to display the correct data, and I am able to edit each post individually. What I don't know how to do is pass in the multiple post IDs as an array, so I can then process them in a loop. Is there a way to do this without creating a new Post model, i.e. without saving the user's posts to my database? Right now, I just have a User model.
I think I figured it out.
In my form_tag, I collect the post IDs in an array via checkboxes:
<%= check_box_tag "post_ids[]", m["id"] %>
and in my controller, I have an action that pulls in those post IDs like so:
def edit_tags
#user = User.find(params[:id])
#posts = params[:post_ids]
end

Creating a specialised view filtering form in Rails

G'day guys, I have a current set of data, and I generate multiple analyses of this data (each analysis into its own active record item called a pricing_interval) using a helper function at the moment.
Currently to analyse the set of data, you need a start time(using datetime_select) an integer (using text_field) and a name (using text_field)
I would like on submission of the form to be redirected to the index page of my pricing_interval, as the values will be re-generated. Manually generating a range proves that my helper methods work.
How would I build a form that on submit would send parameters to a function in the form of (date,integer,name) so that it could immediately begin work whilst redirecting the user to server/pricing_intervals
Anything at all would help, I've spent hours over the past few days trying to get the rails form syntax working properly to no avail, a really straightforward guide to what I would implement to get this working would be amazingly appreciated.
I've looked through the form guides, as I'm not creating an object, but merely parsing params, there's got to be an easy way to do this, right?
You can use the rails form helper hidden_field_tag to put in whatever form fields you want. They are added to the params hash that your controller will see. You don't have to only send form fields that correspond to an ActiveRecord model.

Resources