Rails 3 Getting different request from same url depending on action - ruby-on-rails-3

I am creating an ajax request and building the url as follows:
function submitYear(year){
new Ajax.Request('update_make/'+year, { method: 'get'});
}
When I am at the new action for my car_infos controller, http://localhost:3000/car_infos/new this Ajax request works fine. I get a request that says:
Started GET "/car_infos/car_infos/update_make/2011"
The route matches up and all is well. However, if there is an error in the create the url becomes http://localhost:3000/car_infos and then when my ajax request triggers I get this with a routing error:
Started GET "/update_make/2002"
No route matches "/update_make/2002"
Here is what happens in my controller when create fails:
format.html { render :action => "new" }
I understand why I am getting the routing error, because I don't have a route set up as /update_make/. Here is my route.
match 'car_infos/update_make/:year', :controller => 'car_infos', :action => 'update_make'
So two questions.
Why does my get request change when the url changes from car_infos/new to car_infos
How do I resolve this so when I create the url in the javascript it works for both cases? I don't think putting a route for /update_make is the answer. If I redirect to /new then I lose the field values and the error message.
Thanks,

You're sending your ajax request to a relative path, so if your browser location path changes the request path changes too. The browser location path could be changing because of a redirect in Rails or in javascript.
Is this a typo (the 2x "/car_infos")?
Started GET "/car_infos/car_infos/update_make/2011
You could spell out the absolute url in your javascript:
function submitYear(year){
var absoluteUrl = 'http://' + window.location.host +
'/car_infos/update_make/' + year;
new Ajax.Request(absoluteUrl, { method: 'get'});
}

Related

Cypress: Check current URL

I am trying to get URL which I am getting after redirection if do following:
cy.url().then(url => {
cy.log(url);
});
Then I get logged initial URL, but not the (new url), how do I get url in cypress after redirection?
redirection img (new url)
Solution:
To get the redirected URL just simply add cy.get and your element so it will wait until the redirect page loads. Here is the working solution for me:
cy.get('.crumb > p')
cy.url().then(url => {
cy.log(url);
});
Exactly as you wrote, you need to first wait for a element from the new URL to be visible, using a cy.get('any-element-from-the-new-url').
After that you can get the new URL just using cy.url(), in this case if you want to log this URL you can just use cy.log(cy.url()).
A more meaningful approach would be to add an assertion on the pathname to what you are expecting. The docs can shed some light on the cy.location(). You can use an include assertion on the pathname in the situation that you will not know all of the url string.
// visiting url - https://website.com/favorites
// redirects to - https://website.com/favorites/regional/local
cy.location('pathname') // yields "/favorites/regional/local"
.should('include', '/favorites/')
.then(cy.log)
If you do this, you don't have to do any previous step. Especially because the expectedUrl doesn't have to match the actual url which allow your test doesn't crash if that happens.
cy.url('match', 'expectedUrl').then(el=>{
cy.log(el)
})

Express js Redirect not rendering page

I am trying to redirect the user that uses my website after he has logged into his account, he will be redirected to a dashboard.
The problem is that I can see a request for the /dashboard route in the Network tab of the browser Inspection Tools, but the page never loads.
This is my code so far.
router.post('/login', function(request, response){
// verify against database and send back response
var userData = {};
userData.email = request.body.email;
userData.password = request.body.password;
userData.rememberPassword = request.body.rememberPassword;
// check if in the database and re-route
var query = database.query('SELECT * FROM users WHERE email = ?', [userData.email], function(error, result){
if(error){
console.log('Error ', error);
}
if(result.length){
// check if the password is correct
if(userData.password === result[0].password){
// redirect to the dashboard
// TODO this piece of code does not redirect correctly
response.redirect('/dashboard'); // < HERE
console.log('Haha');
}
}
else{
// do something when there is are no results
// invalid email status code
response.statusCode = 405;
response.send('');
}
});
});
And this is the route towards the dashboard, and it is being rendered properly when accessed in the browser.
router.get('/dashboard', function(request, response){
response.render(path.resolve(__dirname, 'views', 'dashboard.pug'));
});
Why can't it redirect when the user completes the login process?
Thanks.
The issue can be in the frontend and not in the backend. If you are using AJAX to send the POST request, it is specifically designed to not change your url.
Use window.location.href after AJAX's request has completed to update the URL with the desired path.But the easiest way would be to create a form with action as /login and use the submission to trigger the url change.
To make sure that the problem does not lie with the backend,
Add a logging statement to router.get('/dashboard' to verify if
the control was passed.
Check that the HTTP status code of the /login route is 302 indicating a redirect and location header has the route /dashboard.
Content type of response /dashboard is text/html.If not please set it using res.setHeader("Content-Type", "text/html").

issue post a resource with lucadegasperi/oauth2-server-laravel

Im having a problem for creating a resource, when i make a POST request to my route.
My route looks like:
Route::group(array('prefix' => 'api/v1', 'before' => 'oauth'), function()
{
//Media Route
Route::resource('media', 'PostController',['except' => ['create', 'edit']]);
});
when i post the resource it says i should provide an valid access token, but i already doing that. This is the resource uri.
appname.dev/api/v1/media/?access_token=ksfdkfjkfsj
even with:
appname.dev/api/v1/media?access_token=ksfdkfjkfsj
it doesn't work it throws an error with json response
"error": "invalid_request",
"error_description": "The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the \"access token\" parameter."
I'm testing this with the Postman plugin in chrome and using the lucadegasperi oauth2-server package for laravel.
What am i doing wrong?
Thx in advance

What's the easiest way to display content depending on the URL parameter value in DocPad

I'd like to check for an URL parameter and then display the confirmation message depending on it.
E.g. if I a GET request is made to /form?c=thankyou docpad shows the form with thank you message
I think there is two basic ways to do this.
look at the url on the server side (routing) and display differing content according to URL parameters
Look at the parameter on the client side using JavaScript and either inject or show a dom element (eg div) that acts as a message box.
To do this on the server side you would need to intercept incoming requests in the docpad.coffee file in the serverExtend event. Something like this:
events:
# Server Extend
# Used to add our own custom routes to the server before the docpad routes are added
serverExtend: (opts) ->
# Extract the server from the options
{server} = opts
docpad = #docpad
# As we are now running in an event,
# ensure we are using the latest copy of the docpad configuraiton
# and fetch our urls from it
latestConfig = docpad.getConfig()
oldUrls = latestConfig.templateData.site.oldUrls or []
newUrl = latestConfig.templateData.site.url
server.get "/form?c=thankyou", (req,res,next) ->
document = docpad.getCollection('documents').findOne({relativeOutPath: 'index.html'});
docpad.serveDocument({
document: document,
req: req,
res: res,
next: next,
statusCode: 200
});
Similar to an answer I gave at how to handle routes in Docpad
But I think what you are suggesting is more commonly done on the client side, so not really specific to Docpad (assumes jQuery).
if (location.search == "?c=thankyou") {
$('#message-sent').show();//show hidden div
setTimeout(function () {
$('#message-sent').fadeOut(1000);//fade it out after a period of time
}, 1000);
}
This is a similar answer I gave in the following Docpad : show error/success message on contact form
Edit
A third possibility I've just realised is setting the document to be dynamically generated on each request by setting the metadata property dynamic = true. This will also add the request object (req) to the template data passed to the page. See Docpad documentation on this http://docpad.org/docs/meta-data.
One gotcha that gets everyone with setting the page to dynamic is that you must have the docpad-plugin-cleanurls installed - or nothing will happen. Your metadata might look something like this:
---
layout: 'default'
title: 'My title'
dynamic: true
---
And perhaps on the page (html.eco):
<%if #req.url == '/?c=thankyou':%>
<h1>Got It!!!</h1>
<%end%>

Error using Laravel resource controller update method within a grouped route

I have a Laravel app which I'm using to create a RESTful API.
I'm having some issues with the edit/update methods in my resource controller when using a grouped route.
WHen I remove the route grouping the controller works as expected.
My routes look like:
Route::group(array('prefix' => 'api/v1'), function()
{
Route::resource('/trip', 'TripController');
});
So using this my RESTFul endpoints should look like:
GET /api/v1/trip/{resource}
GET /api/v1/trip/{resource}/edit
PUT /api/v1/trip/update/{resource}
GET /api/v1/trip/create
POST /api/v1/trip/store/{resource}
etc.
All work except the Edit and Update endpoints.
When I hit /api/v1/trip/{resource}/edit I get the following error
Unable to generate a URL for the named route "trip.update" as such route does not exist.
My Controller Edit method is just a quick form for testing. It looks like:
public function edit($id)
{
$oTrip = new Trip;
$oTrip = $oTrip->find($id);
echo Form::model($oTrip, array(
'route' => array('trip.update', $oTrip->id),
'method' => 'PUT'
));
echo Form::text('headline');
echo Form::text('description');
echo Form::submit('Click Me!');
echo Form::close();
}
Laravel seems to be unable to find trip.update when in the grouped route.
I think there is a problem with the Group prefix api/v1
When I comment out the group code just to read:
Route::resource('/trip', 'TripController');
...and access the uri just as /trip/{resource}/edit without the api/v1 prefix everything works fine as expected.
Is there something I'm missing?
I would really like to get this working with the grouping and api/v1 prefix.