How to close the window in Rails? - ruby-on-rails

I have the following link in my view that opens a changelog in a new page:
<%= link_to("Changelog", changelog_path, :target => "_blank") %>
In this changelog page I would like to create a link to close this changelog page.
What is the most appropriate way to do this ?
Is that possible to achieve this without Javascript ?

You will need to use JavaScript for this. You can call window.close(), but this will prompt the user before closing the window, unless the window was opened from JavaScript with window.open(), so you will also need to use JavaScript for opening the window instead of a link with target="_blank".

Related

Why does adding ":remote => true" cause my file download not to launch?

I’m using Rails 5. I have set up this link for downloading a file
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), :remote => true %>
which links to this controller method
def download_cc
scenario = Scenario.find(params[:scenario_id])
send_data scenario.cc_data, filename: "#{scenario.title}.imscc", type: 'application/zip', :disposition => 'attachment'
end
But when I click on the link above, even though I see a remote call being made, no file download appears in my browser (I’ve tried this on both Chrome and Firefox). How do I make this work? Note, removing “remote => true” is not an option because then the URL in my address bar changes, which I don’t want.
I've not seen the remote option used with file downloads. It's mostly designed for AJAX form posting and URL hits like deleting a record or action calls that make sense to do over AJAX.
If you want the link to force a "Save As..." dialog, use the download attribute in the link. A 'bare' download attribute will force the "Save As..." dialog. Its value will be the download filename. So <a href="/my/download/action" download="file_100.zip"> opens a dialog prompting you to save file_100.zip.

QR code link to a modal

Is it possible to create a QR code that redirects the user to a modal in a specific page?
As in, you send him to the page X but already trigger click <%= link_to "modaltoshow %> , and the user sees the modal immediately after scanning.
Thanks
If your question is simply "can I launch a page with a modal already open," the answer is yes. You'd just need a javascript that launches it on load, like the selected answer here.
Assuming you need some ruby logic, you could simply change the target of that script with embedded ruby based upon a parameter the QR code passes (for instance, if you have a parameter "target_modal" that is the target modal's id, just reference that in the script). It could look something like this in your script block:
<!-- The if statement just checks for the parameter. If the parameter will
always be present, you may remove this. -->
<% if params(:target_modal) %>
$(window).load(function(){
$('<%= params(:target_modal) %>').modal('show');
});
<% end %>

Upload an image without displaying file_field and "apply" button. What's the correct way?

I am working in a rails 3.2 app,where the user has the avatar picture.
The user can chose his avatar in these way:
1) chose the file clicking the "Browse..." button in the edit user view (using file_field)
2) chose the file and click OK
3) then click on the "Apply Photo" button (on the right of the field containing the filename) to load the image to the server and update the view
I want improve the user experience, removing the field and the button in the view. This should be the way:
1) click a link 'Update avatar' (or directly click on the picture)
2) chose the file and click OK
No (visible) fields and no Apply button
What's the best way to do that ?
should I continue to use the file_field in the view but hide it and trigger the user#update action using some javascript code ? or is there some other better way to do that ?
Thank You
You can use one of js plugins to upload files. I.e. if you are using jQuery, you can try using plupload
I have encountered this issue with hiding the input element and putting a label instead.
For eliminating submit button, I have added onchange method.
<%= file_field_tag :file, class: "d-none", onchange: "this.form.submit()" %>
<%= label_tag :file, role: "button" do %>
<%= image_pack_tag('logo.svg') %>
<% end %>
PS: I'm using bootstrap class here, styles can be used instead for hiding the element.
The button near upload field is generated by browser and is standart widget for browser. You can use js plugin for that or realize iframe upload method (allow you async file upload even on IE). Simple tutorial for iframe - there Iframe method allow to you to customize upload link/field as you want =)

Ruby/Rails - Open a URL From The Controller In New Window

I'm in my applications controller and I have a url string. How do I tell the app to open the url in the browser in a new window. Is there a way to set the target to blank?
For example
def link
#url = 'www.google.com'
****??? Open #url ?? *** with target blank?
end
This is not possible to do directly from the controller. Using redirect_to #url has the effect of opening an URL in the same "window", as it simply sends a HTTP redirect instruction back to the browser. redirect_to is not capable of opening new windows. The controller resides on the server side, and opening a new window belongs to the client side.
Some options:
a) render a link with <%= link_to 'Google', 'google.com', :target => '_blank' %> or Google which the user can click on in a new view
b) use JavaScript to open the link automatically, but beware that browsers may treat this as a popup and block it
By combining these options you can open links in new window for browsers/users who allow it, and fall back to a regular URL in case that didn't work.
As the others point out, you can't (and shouldn't) do this in the controller. In the view you can use
<%= link_to #url, :target => "_blank" %>
Well, it's not that you CAN'T do it, it's just kind of a convoluted process. I did this in a project I'm working on. Basically, it's a mixture of Rails goodness and Javascript. I simply passed a flash notice on creation of an instance, and then used a script to set that flash notice equal to a js variable, and a redirect_url. If that particular flash notice pops up on that page, it redirects in the js script. Like I said, it's hack hack hack, but it works for my purposes.
You can't do that in rails, because your script is being executed on a server.
Use Javascript to work with browser on the client side.

Add a modal dialog into Rails?

I want to add a Ajax link into my application. When I click on the link it should pop up a modal dialog which contains event's details..
I add this link in my helper class.
link_to(event.name, events_path(#event), :remote => true
Then I create js file to insert content into hidden div.
$('.modal').html(<%= escape_javascript(render(#event)) %>);
$('.modal').dialog();
In here modal is my hidden div. But it could not pop up any modal dialog. I cannot understand what is the error and why is it not working. Plz can anybody help me to correct this?
Change
$('.modal').html(<%= escape_javascript(render(#event)) %>);
to
$('.modal').html("<%= escape_javascript(render(#event)) %>");
From a JS point of view, your code will be invalid because you're not wrapping your render in quotes and it will try to evaluate your HTML.
EDIT
If you're trying to link to this on a show click, you'll need to use show.js.erb to show your modal dialog rather than create.js.erb. create will only be called if you POST a form to /events whereas here it looks like you're trying to show just the details of the event.
Put the code above (with quotes) in the show.js.erb, make sure you have a respond.js response in your show method on the controller, and try it again.
Could be possible that you didnt install Jquery since the rails default is Prototype library. I would upgrade to Rails 3.1 which makes it easy to use jquery:
rails new example -j jquery
or install jquery:
http://railscasts.com/episodes/136-jquery

Resources