Is this a valid Sinatra route handler? - ruby

Is this a valid route handler?
post '/lists/:id/delete' do
#delete list
...
end
I can't get it to fire.

Yes, it is. But it will only fire when you do a HTTP POST request, e.g. submit a form using the post method:
<form action="/lists/17/delete" method="post">
...
</form>
If you enter the same URL in you browser, however, a HTTP GET request will be fired. If this is what you want, you should replace post with get in your route.
A great place to start: http://www.sinatrarb.com/intro.

Related

Django - AJAX - Why do I need url parameter?

It's my first time using AJAX and I don't understand why I need to specify url parameter in a JS Ajax call.
{% block javascript %}
<script>
$("#id_username").change(function () {
$.ajax({
url: '/some_new_url/',
data: {
'something': ...
},
success: function (data) {
if (data.is_taken) {
alert("Data is already in DB");
}
}
});
});
</script>
{% endblock %}
To my understanding, AJAX is used to do something on the server side without refreshing a page. So it shouldn't redirect to a new url upon sending a data to the server, and stay on the same url. And yet AJAX call requires url parameter.
And I don' really like this, because setting a new url means I have to add another url pattern in my app/urls.py.
re_path(r'^create/$', views.Some_View.as_view(), name='create'),
And as a consequence, make another view in my views.py
class Some_View(ListView):
model = SomeModel
fields = '__all__'
But, I already have a CBV that generates form fields on the user side and accepts user inputs. I only want to make my existing CBV to save data to DB using AJAX call.
Since I don't understand what the purpose of the url is, I don't know how to set up my new url pattern, and CBV. Can I get some explanation here?
++ This is just a bonus question, but my ultimate goal is to generate multiple form fields, and multiple Submit buttons that sends the respective form input data to the server using AJAX. If there's any advice on how to tweak AJAX code, I would appreciate it.
An AJAX request is just a regular HTTP request to a url on the server. The only difference between an AJAX request and a request made by an ordinary browser GET or POST is that with AJAX, the results that come back from the server are returned to your javascript function and then you get to decide what to do with those results.
So there's no automatic updating of anything.
If you want to save something on the server, you need a view there on the server which is capable of understanding the data you are sending in the AJAX request, saving it, and then sending back a response which, again, your javascript code needs to be able to understand.
But if you already have a view which is capable of doing what you want, you can use it for your AJAX request, you just have to send a request with everything in it that the view requires.

Is there a Lemoon route for /{slug}

I'm looking for an existing route (in Mindroute.Lemoon.Helpers.RouteHelper) that will already handle a path like http://www.mylemoonsite.com/blogpost3. It doesn't appear by requesting that URL that such a route is active, but it looks like some of the routes in RouteHelper.cs are attempting to cover that case. I can request http://www.mylemoonsite.com/blog/blogpost3, but I'm looking specifically for the former. Thanks.
Lemoon adds the catch-all route {slug*} to the end of the route-table, effectively catching everything that is not handled by other route handlers such as custom Controllers etc.
In order for Lemoon to respond to the request http://www.mylemoonsite.com/blogpost3 you need to have a page in your site with the permalink blogpost3. Since you get a response when requesting http://www.mylemoonsite.com/blog/blogpost3 I am guessing the permalink for your page is blog/blogpost3.
There are 2 things you can do to get a response from the path http://www.mylemoonsite.com/blogpost3.
Edit the permalink of the page
Add an alias to the page
If you add the alias blogpost3 the page will respond to both http://www.mylemoonsite.com/blogpost3 and http://www.mylemoonsite.com/blog/blogpost3. When adding the alias you can also specify the HTTP response code for the alias (200 OK, 301 Moved or 302 Found).

Sinatra - How to best move variable/parameter between pages

I have a register page with the usual email,name,password ..which is validated in the server's submitted route/page. if it fails then I redirect back but I want to fill the values back in the register page..I can put the register form parameters in the session but it will stay there...is there a page memory(a smaller scope than session) just like session which will be just for the next page and then gone/ which is the best way to implement this.
Thanks
Why don't you just render the registration page from the POST route like this:
post '/register' do
#registration_data = params[:stuff] # store all your registration data
if info_validates # everything validates
redirect './user_home'
else # something fails validation
haml :register # or erb or whatever your template engine is
end
end
Then in your view, have it fill in #registration_data if it exists.
Also, you can clear session data with session.clear.
Ajax validation would be much easier. You just register an onclick event to your form submit button that makes a call to a page that returns a json status code with the error information or 200 for OK. If 200, then submit.

Sending a DELETE request from Sinatra

I am trying to develop a RESTful Sinatra application. Now, I know how to respond to a delete request with something like
delete '/user/:id' do |id|
#do something in the model
end
What I am interested in is how do I get to execute that method. I can't have link that does a DELETE instead of a GET, can I?
The only solution I found so far is sending a DELETE request via jQuery: How to send a PUT/DELETE request in jQuery?
I tried looking into different RESTful Sinatra projects on github but my Ruby knowledge is probably to limited to get how they are doing it.
Put following line in your code.
use Rack::MethodOverride
It will help you interpret post methods with parameter "_method" with value "delete" as put.
Then you can write
delete '/user/:id' do |id|
I thinks it's like the Rails way. You need define a params '_method' with 'delete' value and add it on your form.
When you POST you form with this particular params, you do a DELETE request in sinatra.
Like :
<form action="/search" method="post">
<div style="margin:0;padding:0">
<input name="_method" type="hidden" value="delete" />
</div>
</form>
It's the same with PUT method
Another way is to use Curl:
curl -X DELETE http://host/user/1
%form{:action => "/note/delete/#{#note.id}", :method => "post"}
%input{:type => 'submit', :name=> "_method", :value => 'delete', :class => 'button'}
You can also trigger the delete route with a button like so
see also Call Sinatra delete route with jQuery for how to do this with jQuery and JSON at the front end and Sinatra on the back end.

redirect ajax requests

i want redirect ALL ajax requests(with X-Requested-With:XMLHttpRequest in header) to action: ajax(string function, string args) . How can i do it?
For example: browser send ajax query with paramerts function=getImage&args=4 to url http://localhost/post/123 but we redirect query to http://localhost/ajax.
Use a custom route and/or route constraint.
Look here for more info.
Probably something like Server.Transfer() would work best

Resources