The GET method is not supported for route ajouter_achat. Supported methods: POST - post

enter image description here
Hello everybody! I got this error while I used the POST method for my route and in the form. Can someone help me? Thank you!
Here is the route:
Route::post('ajouter_benef', [Enregistrement::class, 'store_benef'])->name('ajout_benef');
form method
<form action=" {{route('ajout_benef')}} " method="POST">
#csrf
</form>
I tried to clear the cache but there is nothing else apart from the error about the GET method

Related

The action path of a form in Rails

The explanation below is big, but the question is really simple.
I'm doing a simple form project in https://www.theodinproject.com/paths/full-stack-ruby-on-rails/courses/ruby-on-rails/lessons/forms.
The first part where I am, I need to build a form manually - so that I can see how Rails does a lot for me when I use its helper methods.
I'm stuck in this point:
Specify the method and the action attributes in your tag (use $ rails routes to see which HTTP method and path are being expected based on the resource you created).
The routes.rb file looks like this:
resources :users, only: [:create, :new]
I don't know how to determine which method should I use for the form - post or get.
I don't know how to determine which action path I should use.
I've gone into the internet, Rails guides and etc, and have solved the other topics so far, but for this one I can't get through it.
My form so far:
<form action='/create' method="post" accept-charset="UTF-8">
<label for="username"></label>
<input type="text" id="username" name="username"><br>
<label for="email"></label>
<input type="text" id="email" name="email"><br>
<label for="password"></label>
<input type="text" id="password" name="password"><br>
<input type="submit" id="submit" value="Submit" >
</form>
Once I run it in the server and then submit the form - which I did - I should get:
"Submit your form and view the server output. Oops, we don’t have the right CSRF authenticity token (ActionController::InvalidAuthenticityToken) to protect against cross site scripting attacks and form hijacking. If you do not get an error, you used the wrong method from step 1."
Yes, Rails is smart) When u are sending the request to your server, Rails must know from where this request is coming. In short, if your form sending the CSRF token then Rails understand that u send this request, otherwise someone else on your behalf (CSRF attack).
To fix this bug u need to set <%= form_authenticity_token %> in your .erb view. It will generate this input that is below
<input type="hidden" name="authenticity_token" value="your_token_generate_by_rails">
Or for your testing purposes, u can use this in your controller which is processing your request. But never use it in future if you are don't know what are u doing)
skip_before_action :verify_authenticity_token
Hi there fellow Odin student!
It looks like you're doing this lesson. Me too!
It sounds like you're asking about what value to use with action attribute in the form.
When I created a form using the Rails formHelper methods, I inspected the HTML that Rails created when I was previewing the app/website (rails s). By opening up the Developer tools (F12 key, or right click the mouse and choose inspect)I was able to see that the form Rails created had an action equal to the name of the resource, with a forward slash in front of it.
So for your example, the resource created is "users". So the action attribute would be /users
to be complete, the solution would be something like: <form action="/users"...
Hope this helps!

Thymeleaf: Meaning of action parameter in form which already has th:action

I´m just starting with Thymeleaf and have a code example with a form like this:
<form action="#" th:action="#{/register}" method="post" th:object="${user}">
I understand th:action have the controller´s path to send a populated user object, but I can´t figure out why to use action="#".
Please enlighten me.

Laravel 5.5 post request error

I'm working on the project using Laravel 5.5. I have resource controller and trying to save data in store method. The route looks like this:
Route::resource('post', 'Post\PostController');
This is Store method in the controller:
public function store(Request $request)
{
dd($request);
}
And also I have got blade with form like this:
<form action="/post" method="POST">
{{ csrf_field() }}
But I have error and don't understand what is the issue. Clicking on submit, I see this error:
The page has expired due to inactivity.
Please refresh and try again.
Also I see 419 status error in the google chrome console.
By the way, if I change form method from post to get,and also change url, it works.But using non resource controller with post method it does not work again.
Can someone tells me what's wrong here, thanks.
Just want to remind that the problem isn't solved yet. Any suggestions?

rails routing difference between v3 and V5

I have to migrate a Rails app from 3.0.6 to 5.0 and am stuck - in the older routes.rb the only route was the ill advised:
match ':controller(/:action(/:id(.:format)))'
The application has no index method but does use an index.html.erb that puts up a form that renders to:
<form accept-charset="UTF-8" action="simple/rslts" controller="simple" method="post">
This executes a rslts method that uses rslts.html.erb and works using parameters passed in from the form.
I move this exact same code to the v5 system and specify the routes as:
Prefix Verb URI Pattern Controller#Action
simple GET /simple(.:format) simple#index
POST /simple(.:format) simple#rslts
root GET / welcome#index"
It runs the index.html.erb and I get this in the rendered form:
<form controller="simple" action="simple/rslts" accept-charset="UTF-8" method="post">
It fails saying it can't find a route for /simple/simple/rslts
I change the code and now render:
<form controller="simple" action="rslts" accept-charset="UTF-8" method="post">
It fails saying it can't find a route for /simple/rslts but as you can see from the routes I listed that does exist.
I have munged this around a dozen ways and read the routing guide with no enlightenment. This is only controllers and views, nothing else. I hope someone can help me.
Re: /simple/simple/rslts: Your action attribute is just "simple/rslts" which is relative to the current URL. You'll need to prepend a slash, e.g. action="/simple/rslts".
The route /simple/rslts is not defined according to your rake routes output: the right-hand side that says simple#rslts is the controller and action name, not part of the URL (which is on the left and has an optional format specifier). Instead you have defined that POST /simple should direct to the rslts action. You'll need to add a route for /simple/rslts or change the action (again) to post directly to /simple.
post '/simple/rslts' => 'simple#rslts'

Insert cakephp POST params into URL

I have this form below which contains two checkboxes to sort some products:
<form id="FiltreExtraForm" action="" method="post" name="FiltreExtraForm">
<input id="ProductsDeliveryPrice" type="checkbox" value="1" name="data[Products][delivery_price]"/>
<input id="ProductsPicture" type="checkbox" value="1" name="data[Products][picture]"/>
</form>
After POST I do the filtering but I also want to add received parameters to URL E.g: /products/index/delivery_price:1/picture:0 . Is this possible. How can I do that?
Note: I don't want to use GET to send form info.
Sounds like you are looking to do a Post/Redirect/Get.
Here are two examples of doing this in CakePHP:
Searching on surname
Searching on multiple fields
The two main advantages of redirecting a POST to a GET request are:
Users don't get the "Do you want to resubmit?" dialog if they refresh
The resulting page/query can be bookmarked
In the action to which you post, you could simply prepare the GET url and then redirect to this url. The action for that url then does the filtering.
If I understand you correctly (and I'm not sure that I do), you can pass additional variables on the query string of the form's action quite easily. Conventionally, that might look like this:
<form id="FiltreExtraForm" action="/products/index?delivery_price=1&picture=0" method="post" name="FiltreExtraForm">
Using Cake, you should be able to do the same without the traditional query string if you'd rather (though the traditional method above will also work):
<form id="FiltreExtraForm" action="/products/index/delivery_price:1/picture:0" method="post" name="FiltreExtraForm">
I would recommend looking at the form helper or at least constructing the action URI using helpers, but this should get you what you're after.

Resources