Ruby Sinatra Routes Not Being Recognized - ruby

I'm having some issues with Sinatra not recognizing my routes (all other routes work fine, it is just this specific set of 4 routes). The layout is: in my views, the user clicks on a filter button which corresponds to the route of /all /inactive /active or /expired, and should filter out based on this selection.
Here is the index.haml filters:
%form{method: 'GET', action: "/showings/all"}
%input#all.btn.btn-md.btn-secondary{type: 'submit', value: 'All'}
%form{method: 'GET', action: "/showings/active"}
%input#active.btn.btn-md.btn-secondary{type: 'submit', value: 'Active'}
%form{method: 'GET', action: "/showings/inactive"}
%input#inactive.btn.btn-md.btn-secondary{type: 'submit', value: 'Inactive'}
%form{method: 'GET', action: "/showings/expired"}
%input#expired.btn.btn-md.btn-secondary{type: 'submit', value: 'Expired'}
%form{method: 'DELETE', action: "/showings/delete", style: "padding-left: 45%"}
%input#delete.btn.btn-md.btn-danger{type: 'submit', value: 'Delete Expired Hosted Screenings'}
However, in my showings_routes.rb file in my controllers folder, I tried these route but they never get registered (I've tried restarting the web app):
get '/all' do
p "ALL ROUTES"
haml :'showings/index'
end
get '/active' do
p "ACTIVE ROUTES"
haml :'showings/index'
end
get '/inactive' do
p "INACTIVE ROUTES"
haml :'showings/index'
end
get '/expired' do
p "EXPIRED ROUTES"
haml :'showings/index'
end
delete '/delete' do
p "DELETE ROUTES"
redirect '/showings'
end
I'm planning on adding the filter logic later, the routes are just not being recognized when I click one of the buttons or navigate to localhost:3000/showings/all. I'm just confused because this is a built web app, so all the other routes in the showings_routes.rb file work fine (like /create /submit etc.)
Is there a thing that I'm missing when I'm handling my routes here in my application?

Related

Ruby Sinatra URL keeps appending

I have four buttons, and everytime I click the button it should send a post request (I'll show my code below).
This is my books controller.rb
post '/all?' do
#books = # Books here
haml :'books/index', locals: {books: #books}
end
post '/fantasy?' do
#fantasy = # Fantasy books here
haml :'books/index', locals: {fantasy: #fantasy}
end
And this is my index.haml file:
%form{method: 'POST', action: "books/all"}
%input.btn.btn-md{type: 'submit', value: 'All Books'}
%form{method: 'POST', action: "books/fantasy"}
%input.btn.btn-md{type: 'submit', value: 'Fantasy'}
However, what happens is the following:
(1) URL: localhost:8080/books
(2) I click on the all button
(3) URL: localhost:8080/books/all
(4) I click on the fantasy button
(5) URL: localhost:8080/books/books/fantasy
This is the problem - how can I get it so on the second button click, it goes to localhost:8080/books/fantasy instead? I'm sure its a simple fix just not sure how. Thanks.
I think the recommended way is to use absolute paths with the addition of the url helper. This should better future-proof your code in case it is mounted in a sub folder or behind a reverse proxy.
%form{method: 'POST', action: url("/books/fantasy")}

How redirect from controller side after Ajax call in Rails 6?

I want to know if it's possible to redirect with controller from an ajax request in Rails 6 ?
I tried to use
redirect_to "url"; return and render :js => "window.location.reload" don't work for me :(
Thank you for your help :)
This works for me:
window.location.href = '/path'
Example:
$.ajax({
url: uri,
type: "POST",
data: form,
dataType: 'json',
processData: false,
contentType: false
}).done(function(e) {
window.location.href = '/project_proposals'
})
Do it in your controller.
render js: "window.location='#{goal_path(#goal)}';"
Ideally you'll want to keep as much business logic out of your JS as possible for rails apps. You also can't cleanly use your route helper methods in js. You can however setup your "redirect" in your controller.
app/view/controllers/goals/adopt_controller.rb
# :nodoc:
class Goals::AdoptController < ApplicationController
def update
# business logic....
# "redirect" the client
render js: "window.location='#{goal_path(#goal)}';"
# or make a reusable js view. this will search a wide variety of possible file names. In this case it'll match /app/view/appliation/redirect.js.erb
# #to = goal_path(#goal)
# render 'redirect'
# the above lets rails guess what you want to do. this is a bit more explicit to only render this view is the client can accept a js response. Other scenarios will throw an error
# #to = goal_path(#goal)
# respond_to do |format|
# format.js { render 'redirect' }
# end
end
end
/app/view/appliation/redirect.js.erb
window.location='<%= escape_javascript to %>';

How to use ajax in Padrino framwork

I am a newbie of web develop, I am learning padrino framwork. But offical guide is lack of ajax content. Can any one supply me a doc or example for ajax in padrino?
eg,modifiy div .
I wrote a app,but ajax dont works fine.The refresh.js content is displayed on the #cn-status div.Followin is my ruby program
#controller
#----------------------------
get :refresh, :provides => :js do
if request.xhr?
# refresh.js.erb is js file for modify div content
render "aj/refresh", :layout => false
else
redirect url('aj/')
end
end
#link on the other erb file
<li><%= link_to 'get', url(:aj, :refresh, :format => :js), :confirm => "Are You Sure?", :remote => true %></li>
#refresh.js.erb file
$(document).ready(function(){
$("#cn-status").load("/cj/refresh_codename_get",function(responseTxt,statusTxt,xhr){
if(statusTxt=="error")
alert("Error: "+xhr.status+": "+xhr.statusText);
});
});

jQuery Mobile breaks Rails respond_to when using UJS remote links accept headers

I'm converting our Rails 3 web app to use jQuery mobile, and I'm having problems with "remote" links.
I have the following link:
= link_to "Text", foo_url, :method => :put, :remote => true
Which, on the server, I'm handling like this:
respond_to do |format|
if foo.save
format.html { redirect_back_or_to blah_url }
format.json { render :json => {:status => "ok"} }
end
end
This used to work wonderfully. However, since I've added jQuery Mobile, the controller code goes through the "html" branch instead of the "json" one, and responds with a redirect.
I've tried adding
:data => { :ajax => "false" }
to the link, but I get the same effect.
Before jQuery Mobile, UJS was sending the request with the following accept header:
Accept:application/json, text/javascript, */*; q=0.01
while with jQuery Mobile, I'm getting this header:
Accept:*/*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript
I believe this change in headers is the culprit of the change in server-side behaviour. I haven't been able to debug through the client side to figure out who's doing what exactly. UJS is clearly still doing something, since I'm getting a "PUT request" of sorts, things get routed appropriately, etc, but I'm not sure what's changing the headers.
Thank you!
Daniel
By default remote: true goes to the format.js clause (and searches for some .js.erb template to send back), and defaults to format.html and sends back the html template.
You should use ”data-type” => :json in your link_to call if you want to return json, like:
<%= link_to 'Show Full Article', #article, :remote => true, "data-type" => :json %>
Source: http://tech.thereq.com/post/17243732577/rails-3-using-link-to-remote-true-with-jquery-ujs

Rails 3 Image upload form submitting with http instead of AJAX

I am working on a form to submit it by AJAX instead of http.
This is the form :
<%= form_for(:image, :remote => true, :url => {:controller=> 'questions',:action => 'upload'},:multipart => true) do |f| %>
<%= f.file_field :image, :onchange => "$(this).parents('form').submit();" %>
<% end %>
I have set the :remote => true option above and submitting the form with an onchange event . I have the following code in controller :
def upload
if request.xhr?
#image = Image.new(params[:image])
#image.save
respond_to do |format|
format.js { render :layout=>false }
end
else
render :text => 'Request Wasnt AJAX'
end
end
My action renders the text everytime , the request does not seem to be AJAX style despite the remote tag being set (it appears correctly even in the final HTML). I can't figure out where I am going wrong with this . I have tested it in the latest browser version of FF and Chrome , so I don't think it's a browser issue. Any ideas ?
Update : I did some more debugging attempts . The issue is with the file field , if I replace the file field with text field , the request is AJAX (everything else remaining same) . But with a file field it always sends a non AJAX request.
Note : Overall objective is to upload an image via AJAX request, with the response rendering nothing, no HTML, no redirection, no reload of the page.
Got it to work by installing the Remotipart gem . To upload image files using ajax form submission , this is the only way . Find the git here :https://github.com/JangoSteve/remotipart
JQuery form.sumbit() submits a form the normal way (Not Ajax). You have to do it differently:
$('#submitButton').click( function() {
$.ajax({
url: 'some-url',
type: 'post',
dataType: 'json',
data: $('#myForm').serialize(),
success: function(data) {
// ... do something with the data...
}
});
});

Resources