Recently I downloaded a html template hoping I might be able to use it with rails. But I am stuck at the form template itself. I know how to use form_for and simple_form_for. But I don't know how to make a nice UI so is using the template.
A part of the form is
<form class="form-signin" action="sessions_path" method="post">
But this gives error saying [GET] "/sessions_path" routes not found. Why is this happening?
But when I use form_tag instead of form the sessions_path works fine.
I googled this ,couldn't find any related posts.
It's because form tag can use the rails route, while in pure html you will not have access to it.
Check out http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag to see what the form_tag function actually generates.
In your case it would should probably be:
<form class="form-signin" action="/sessions" method="post">
Related
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
Hi I am working with Laravel and I have a question about the . Back in time we used to write something like the following when we want to submit data.
<form action="insert.php" method="POST"> </form>
Now, I have seen in Youtube videos and here in stackoverflow many code snips where we use the following:
<form action="{{URL::to('/insert')}}" method="POST">
I would like to know what is the difference? In the second way /insert is pointing to a file or a controller? The first one is wrong? Or it is just an alternative old fashion way?
Also I have seen two ways of inserting token. Which is the best? What are the differences? Both work the same? What will happen if I do not insert a token?
{{csrf_field()}}
#csrf
Thank you for your time!
Well, when you do this:
<form action="insert.php" method="POST"> </form>
What you are doing is telling the html markup explicitly the relative path to post the data, in your case index.php
Say, you were on this url in your app: http://myapp.com/some-page and you were click the submit button, what will happen is that it will post the data to this relative url: http://myapp.com/some-page/index.php
Now, the reason why we use the URL helper facade is to make urls relative to the application's url.
For example, if you've defined in your application config (or .env) that the APP_URL is equal to something like http://myapp.com then when you use this: URL::to('/insert') it will output the following url: http://myapp.com/insert - regardless of which url you are on.
Hope this makes sense. As for this:
{{csrf_field()}}
#csrf
I believe they achieve the same, they generate an hidden input field with your current csrf token in it.
I added postmark to my Ruby On rails app. I followed the steps from the page https://devcenter.heroku.com/articles/postmark#sending-emails-in-ruby-on-rails-3-x . Now I need to add the following code:
class SuperMailer < ActionMailer::Base
def email
from "test#domain.com"
subject "Hello"
recipients "myemail#domain.com"
tag "big-bang" end
end
However, I do not know where to add this class, and how to use it. I Do I add it in the application.rb file in the module of my app?
And how do I use this class to send the email on after submit (i.e. when someone presses Submit)?
I wrote the form with the submit button in an html file in my static_pages under app > views
Do I do the following?
<form name="contactform" method="post" > ...
The Action Mailer Rails Guide Is the best most comprehensive guide on this - it's easy to read and well worth your time.
As to your question - it's waaay too vague to answer (you have asked multiple questions here, not just one)- but in this case, all the answers are in the Rails Guide.
but for one: you put mailers in the app/mailers directory
and two: you have to set up a controller action for your form-action that calls eg: SuperMailer.email.deliver
but really: read the guide it will answer your questions
I am getting intermittent CSRF token authenticity errors. Specifically, it occasionally happens when I submit a regular form via POST. I can get pass this error if I just go to a few other random pages before submitting the form again. This error does not always come up, it just comes up occasionally. It leads me to think that maybe the csrf meta tags being generated are not always valid.
I have already included the following statement in the header of application.html.erb
<%= csrf_meta_tags %>
I also have the following in application_controller.rb
protect_from_forgery
Is there anything else I should be doing?
If you're using the Rails helpers form_tag or form_for to generate your 'regular forms', then you will see if you inspect the HTML that an extra div is generated under the form tag, which contains a hidden field for utf8 compliancy, and an authenticity_token.
If you're writing your own forms (with <form>...</form> or %form) then you will need to manually add the authenticity token.
There's another helper called form_authenticity_token that you can use thus:
<input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>">
But I'd recommend you use the Rails form tag helpers, and avoid adding you own authenticity token fields.
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.