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.
Related
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.
I'm using the "web_app_theme" gem. The problem is that the flash error and warning messages don't work.
For example, in the Sign In page it does work; but in the Forgot Password page it doesn't work. I tested it by displaying the contents of the flash <%= debug flash %>.
This is what I get in the Sign In page...
!ruby/object:ActionDispatch::Flash::FlashHash
used: !ruby/object:Set
hash:
:alert: true
closed: false
flashes:
:alert: Invalid email or password.
now: !ruby/object:ActionDispatch::Flash::FlashNow
But in the Forgot Password page I get...
!ruby/object:ActionDispatch::Flash::FlashHash
used: !ruby/object:Set
hash: {}
closed: false
flashes: {}
now: !ruby/object:ActionDispatch::Flash::FlashNow
I'm thinking that it might be related to not using haml. In the Sign In page I'm using haml but in the Forgot Password page I'm using erb. I'm confused because the contents of 'flash' should be the same regardless of the format, right?
This has little to with the view code you use.
From your question, it's clear that the flash in the Forgot Password page has not been set.
Either that or it has been set and erased. Using HAML or ERB is not relevant since the Rails flash is present in both pages.
Check the controller code for the same and verify what should be in the flash on that page.
Also, while there is no hard and fast rule, it is strongly advised to use only one template / view engine (erb or haml) in a single application.
On the Forgot Password page, is the flash object being set and the page then rendered, or is it being set and then a redirection to the page happening?
The normal use case for flash is that you assign to it and then redirect to another page where flash is present. If you want to use flash and then display it's contents in the same action (such as then rendering a template)
Instead of writing (e.g.)
flash[:notice] = 'Foo Bar'
You write
flash.now[:notice] = 'Foo Bar'
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".
on my web site I want to have login/registration form in modal window done using facebox (jQuery plugin). What is better:
Create view with one method and template that has form and refer facebox to this view.
Create static HTML file in public directory and refer facebox to this static page.
What I want to achieve is:
Easy verification (like "user name already taken", "password confirmation doesn't match password" and stuff like that).
Easy submit and redirect
I'm new to Rails, I just know about forms verification in Django, so for Django I would probably choose option 1, but it might be another thing in Ruby.
If you want the verification to come back to the registration page, you should make it a dynamic page.
The other problem with a static page in the public directory is that your links all become hardcoded, so if you application ever lives off the domain root (i.e. example.com/app) the links in that static file could be wrong.
Additionally, if you ever need to move your images to a different host, you lose the advantages of the image_tag.
Only use static resources if you know things won't change and you need speed. If your dynamic pages are too slow, you can cache them, or you might be doing something wrong.
UPDATE: (to address the first comment)
You can't use the rails functions to build your URLs when you are in the public folder. If you need rails generated URLs in your javascript, trigger them from a rails view page.
Generally, I'll do the following:
In application.html.erb in the head tag:
<%= yield :headScripting %>
Then in the view page that is triggering the javascript:
<% content_for :headScripting do %>
jQuery().ready(function() {
jQuery("#placeholder").load("<%= summary_model_path(#model) %>");
});
<% end %>
That would load the summary text from the model controller action summary. This would probably render :text => "summary" or render :layout => false depending on your needs
i m using rails 2.3.3 and web browser Firefox i have added ajax and java script and it is working too but i have to reload the page every time when i press Add to Cart button to show item additionn in the side bar it don’t show it without reloading.
anyone please help me how can it show item addition in side bar when i press Add to Cart button with out reloading the page?
If you haven't already done so, install Firebug for Firefox, for these reasons:
it'll tell you if you have a Javascript error.
it'll show you if your Ajax request is being received and its contents.
you can inspect your page elements such as the cart to see if it's set to be shown, if the ID is correct, etc. in a much faster way than browsing through the source.
and more (CSS, etc).
If you can't figure it out by looking at the Firebug console, and since you're following a tutorial, why don't you download the Depot source code and compare it with your own to see what you're doing wrong.
If you have the book, the complete source is listed at the end of the book. You can also download the source from here.
The standard ajax helper methods are link_to_remote, form_remote_tag, form_remote_for, button_for_remote. (I might have missed a few.) If you're not using one of them, you could be doing something wrong.
If you're using one of the helper methods with remote as part of the name, you might be missing the update option or the update option is pointed to the wrong html element.
For a form_remote_tag helper:
form_remote_tag(:url => {:controller => controller_name, :action => action_name, :id => id},
:update => element_to_update)
The element_to_update should be the html element's id that you're updating.