How to get info from a form in Rails? - html

I'm new on Rails and web developping.
I wonder how I can get what's written inside a form.
This is what I have :
<form>
<input id="bottom" type="email" name="email" placeholder="email"/>
<input type="submit" value="Ok"/>
</form>
What I want now is to get and save what written in the input, but I don't know how do to do it with rails.

Add a route and map it to the controller. Use the defined route in the action.
in the controller you can access the values as params[:input_name]. Replace input_name with the corresponding values.

I recommend to install 'pry' gem which allows you to write 'binding.pry' somewhere in the code (in your case you should add this line to your controller 'create' action which is responsible for the page where is your form located).
What will happen? Whenever you press submit button, you go to console and see that it shows you a 'create' action. Then you can type 'params' and press 'enter' and you will see how is the data from form stored.
The 'pry' gem is really essential assistant for Rails developing. You can paste 'binding.pry' at any point to check what is happening.

Related

antiforgery token in asp.net core razor pages?

I'm running into an issue I don't quite understand.
asp.net core 2.2.1 using razor pages, I'm having to manually generate the antiforgery token but all the documentation seems to claim that isn't necessary with razor pages.
Any insights as to what I'm doing wrong here?
If you remove the #Html.AntiForgeryToken() from the below form then the token isn't added. If this is what you're supposed to do that's great, I'm done, but every source I can find seems to think this isn't necessary.
<form method="post">
#Html.AntiForgeryToken()
<div>Source Type: <input asp-for="filter.SourceType" value="JsonEvent"/></div>
<div>Source Name: <input asp-for="filter.SourceName"/></div>
<input type="submit"/>
</form>
You absolutely do not need to use code like #Html.AntiForgeryToken() within your form element in order to generate an AntiForgeryToken when you are using ASP.NET Core Razor Pages. The token is generated and submitted automatically when you submit your form.
You can validate this idea by checking your Browser's development tool section. You can inspect the headers and you will see a Form Data "_RequestVerificationToken" as shown in this screenshot.
But, note that, your Ajax requests are different. For example, if you use jQuery Ajax method to post to any of your Razor page's Post handler, then you will need to generate the token explicitly and pass the header along with your request.
As per the Documentation, #Html.AntiForgeryToken() does not need to be added as the markup you used should be enough:
<form method="post">
...
</form>
I would check that the token isn't set at the bottom of the form as outlined in this article

html single submit button used for two different tasks

I've a html button which I want for two different tasks on a single press event. First, when I press the submit button it should insert data to the database (which I've done) and the another task is to render to another page. How can I make this button do these two functions simultaneously?
You can do this by specyfing action attribute of form element:
<form method="post" action="page.jsp?action=save">
...
<input type="submit" value="Submit!" />
</form>
After clicking submit button user is redirected to page.jsp (it may be the same page) and in that file you can get value of action variable: if it's save then execute code responsible for writing to db. After that code you place page-rendering code.
It's the simplest solution, but maybe not the most elegant.
"Just" do it then.
someDAO.save(someData);
response.sendRedirect(newURL);
That's basically all you need to execute in your servlet (or JSP if you're still abusing it as a controller).

How to use the form value in the action?

I am trying to make a form in html that uses the value you enter to form the destination URL.
<form action="../search/${params.q}" method="post" id="q">
Busqueda: <input type="text" name="q" /><br />
</form>
This is what i am doing, but it does not work, any cluess? thanks!
You'd need to handle this using a script - either server-side or on the client (JavaScript).
HTML alone can't handle parameters in the way you're using them.
So you'd need to either POST the form (as you're already doing) and handle the postback by redirecting your request to the new address, or use JavaScript to capture the field's value when a submit button is clicked and loading the new address in the browser window.
I'd suggest server-side is the best option as JavaScript might be disabled or unavailable.

Rails 3, make submit call specific function in controller

new to web development, here. I have a form like this:
<form name="myForm" id="myForm" method="post" >
<select id="id" name="foo">
...some stuff
</select>
<input type="submit"/>
</form>
The submit button calls the 'index' method of my controller, as expected. I would like to make it call some other function, such as 'update', how do I do that? I need to do something with the #_params hash, but I don't want invoke the index function, to do it. Thanks.
You did not include the view logic so I am going to take a stab in the dark and guess you are writing the HTML for the form rather than using the RoR helpers.
In rails there are a set of helpers that help you generate forms and form items.
Please check the docs for form_for
Using form_for will follow the basic restful routing unless you modify the url parameter. So if you are on /new you will be routed to /create, if you are on /edit, you will be routed to /update. More precisely, if the object is new, you will submit to create, if the model exists you will submit to update.
If your form doesn't use a model, you can use the form_tag helper that takes a url parameter and you can pass a string specifying what path to submit to.
If you just need to know how to do this in plain HTML, read this. Essentially, you need to include an action attribute on form that specifies the path to the action you want to post to.

HtmlInputFile through post action

I have a web page which have 3 controls:
form using the post command.
in the form i have an input file control named "myFile".
a button
the upload process works just fine, until I'm trying to post the form and handle the upload in another form.
Request["myFile"] and request.Params["myFile"] gave me nothing
It's the Request.Files collection you need - look it up in the .NET Framework docs:
Request.Files["myFile"]
Also make sure that the enctype attribute of your upload form is set correctly - it should be "multipart/form-data" if the form contains file inputs.
Maybe this will help
<input type="file" size="50" id="ipFile" runat="server"/>
runat="server" gives you access tot the HtmlInputFile structure