I have a very strange problem.
I'm Posting a form with some files to the server but for some reason Laravel is redirecting my request with GET and of course I get a MethodNotAllowedException.
Here is the code for my Routes: Routes
I'm testing the route::post('/admin/article'), just returning all the input
Here is my front-end form: Form
I'm using CKEditor
Every time I include an image inside the editor of "cuerpo", Laravel redirects my request to GET /admin/article, which does not exists.
The funny thing is that if I just input plain text it works fine ....
I've had the same problem, and the solution for me was to use Laravels built-in form opener and change my routes, like this (Adding a named route):
Route::post('/admin/article/', array('as' => 'admin.addArticle', 'uses' => 'AdminController#addArticle'));
And open your form like this (using the named route):
echo Form::open(array('route' => 'admin.addArticle'));
I don't know why this works, and a normally created form does not.
Related
I have an action that I want to use to handle the saving of a report for a project and a sub project. The code to run is essentially the same so I thought I could create a custom save action and then specify the routes; however, it doesn't seem to be working as expected.
My action signature is
[Route("Project/{projectNumber}/Reports/ProjectHoursAndCosts/save")]
[Route("Project/{projectNumber}/Reports/ProjectHoursAndCosts/{subProjectNumber}/save")]
public ActionResult ProjectHoursAndCosts(ProjectHoursAndCostsViewModel projectHoursAndCostsViewModel)
If I remove one of the routes it works as expected. Any ideas?
I've set the action in form
(Html.BeginForm("Save", "Reports"))
If my initial url is
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts
it seems to work ok, however if I try to save a sub project which has the url
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/61033651-1
When I hit save the url changes back to
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/save
I would expect it to submit to the url
http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/61033651-1/save
Update
Looking at the raw form element in the html page I can see that even though the url is http://somehost/somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/61033651-1, the actual post url is still somewebapp/Project/61033651/Reports/ProjectHoursAndCosts/save
How can I change the action attribute of the form to use the correct url?
I'm getting this issue:
Router::connect('/',array('controller' => 'Controller','action' => 'login'));
This will show www.mysite/controller/login as the site URL
I would like to overwrite www.mysite.com/controller/login with just www.mysite.com, but still go to the login page. Does anyone know how to do it with Cake 2.x?
The behavior It's not exactly as you describe.
What the following does:
Router::connect('/',array('controller' => 'Controller','action' => 'login'));
is allow you to type www.mysite.com in your browser, and get the view that www.mysite.com/controller/login renders.
It works like an url rewrite instead of a redirect. Therefore, the above should work as expected. However, if it's not an example, try to name your controller differently, as it may cause trouble with CakePHP.
As stated by Inigo Router::connect() just connects a route/URL to a controller action. So with your defined route you should be able to goto www.mysite.com and your login action will be served (although I'm not sure that it is a good idea to have the base URL act as the login page).
It does not prevent www.mysite.com/controller/login from working as this is one of CakePHP's default routes.
To disable the default routes you need to remove this line from routes.php:-
require CAKE . 'Config' . DS . 'routes.php';
Be warned, if you remove this line you must have defined routes for all your pages in your app's routes file. This is not necessarily a bad thing, Beware the Route to Evil is a good read in regards to this.
As I used the "Auth" component I had to add in the function
beforeFilter()
of my controller this line:
$this->Auth->allow('anAction', 'anotherAction', '**login**');
I would like to create custom SEO friendly routes similar to what is used by http://realestate.com.au For example the following page is shown by google when the search term "real estate melbourne" is used:
www.realestate.com.au/buy/in-melbourne,+vic+3000/list-1
I would like use the following format. mysite.com/trips/search/melbourne-to-sydney/01-01-2011
I have configured the routes in my routes.rb file to get it to pick up the correct parameters when a url is entered is this format.
routes.rb
match '/trips/search(/:fl(-to-:tl(/:tripdate)))' => 'trips#someaction'
My question is how do I setup a form in rails 3 to send a GET request using the above url structure. I have tried playing around with to_params though it seems to then change all my edit, show links etc which is not intended. I could build the link using javascript though I guess this would be a hacky option and the site would not work if javascript was disabled.
Is there a neat way to be able to create a GET submit form in Rails 3.1? The fields are select lists containing name and ids.
Thanks for your help.
This will help you immensely with the friendly URL portion
http://norman.github.com/friendly_id/file.Guide.html
https://github.com/norman/friendly_id
I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?
Using Symfony, I have created a sign-up done page whose URL should be /signup/done
When the form is processed, I want to redirect to the signupSuccess action and pass the recently created ID for future use. So I use...
$this->redirect('#signup_done?id=' . $sign_up->getId());
The routing is as follows:
signup_done:
url: /signup/done
param: { module: SignUp, action: signupDone }
I have avoided the :id at the end because I don't want it in the URL.
But the resulting URL is /signup/done?id=1
Just as an experiment, I tried putting this on a template.
<?php echo link_to('Sign-up again', '#signup_done?id=1', 'post=true') ?>
Even when using post, the query parameter appears in the URL.
The need is: I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?
I set the id as a parameter in the request using $request->setParameter('id', $id) and it was available in the next action.
This kept the URL clean.
If you want to post, you need a form. Back in symfony 1.2 there were helpers that you could call and made you just that - but they were removed for the promotion of promoting good code.
Depending on how you want the 'Sign up again' text to look, you can either create a simple form and a submit button, or create a link, attach a click listener, and create a form there via JS, finally post it.
Any parameter that you pass to the route in url_for, link_to and such end up in the get parameters.
We have the unusual requirement of a multi-step form through GET requests. So, instead of POSTing the forms, we're using GET requests to pass new parameters to the query. So, the basic idea is that there's a treatment, and a date passed to the query. A three-step form if you will.
Show available treatments, pick one
Show available dates (there's business logic in the background that figures these out)
Pick a time
The URL will go through the following states
site.com/bookings/new
site.com/bookings/new/[id|name_of_treatment] (by this, I mean it could either by the ID field or the name of the the treatment)
site.com/bookings/new/[id|name_of_treatment]/2010-12-12/
So, my route looks like this:
map.connect 'bookings/new/:massage_type/:date', :controller => :bookings, :action => :new
massage_type is synonymous with the treatment_id.
So my form_tag looks like this:
<% form_tag( {:action => "new"}, :method => "get" ) do %>
The problem I'm having is that I want it to just give me back the URL site.com/bookings/new/[id|name_of_treatment]/ but instead it gives back the following URL:
http://localhost:3000/bookings/new?massage_type[treatment_id]=1&commit=actionnew
I don't suppose anyone knows?
Forms that use GET are adding the input values as query parameters. There's no way to make the form post to a different URL, where the input values are part of the URL instead - it's just not supported by the HTML standard.
You could use URL rewrite to remap the incoming URLs of this type to the one you want, however that's not really a good solution, because this would result in a second request.
However, what I don't understand is why does the form need to do GET to that specific URL. Is it a requirement that these URLs can be constructed by the user manually, instead of using the form?
If there is no such requirement, I would advise to use standard POST form to http://localhost:3000/bookings/new and modify the form in the response based on the parameters in the POST body, as necessary.
Better yet, write some Ajax that would update the form according to the user's choice, without making a full form submit, until the user has finished all the choices.
By definition, the result of a GET request will have a query string (?param1=value1¶m2=value2&...) in its URL. To get rid of those, you'll have to either start using POST or immediately redirect to the desired URL upon receiving a GET request.
I rather like the redirect approach because it doesn't show that confusing/annoying message about resubmitting POST data when the user refreshes their browser.