(Rails) Using "link_to_remote" in conjunction with a popup/popout window...? - ruby-on-rails

What is the most straightforward mechanism by which to "link_to_remote" and popout a window with the data? I need "link_to_remote" because I need to pass certain data along with the url without redoing my routes. If this would be better served by a "link_to" without the need to redo my routes, I'm all for it. I simply can't get it to work, atm.
Thoughts?

Perhaps you're looking for something like:
<%= link_to_function "Show Article in Popout window",
"window.open(#{article_path(article).to_json}, 'show_article')" %>

link_to_remote is used to do XHR requests, so you cannot use it to open a popup window with the response.
You don't need to redo your route anyway, because I assume that if you want to use link_to* to get some data, then you have your controller/action pair defined and accessible already.
Also, usually, Rails applications have the catch all route enabled to match :controller/:action. If that's your case, then you can use link_to that controller/action to get the data.

What window do u mean ? modal window ? if u want to show some data through modal window, then i suggest u to use http://prototype-window.xilinus.com/index.html, it easier to use, just write some js function and create link_to_function in rails helper/view to call the js function.

Related

Need to use form_tag, but the page is rendered through active_scaffold

The main page is processed and given by active_scaffold. In that case I can't find a place to use form_tag (to make a form_tag in order to pass a FILE object to a controller).
I did go through the wiki page about active_scaffold, and had not figured out a way to replace form_tag..
Really need some help
p params[:record][:excel].original_filename
that works out !

How to navigate?

Say I create an HTML file with two .page on it. In the first .page, I'd like to have a link to the second .page.
Is there a way to navigate between pages without having to write my own JS? This seems to suggest I do have to write JS: http://view.jquerymobile.com/1.3.2/dist/demos/widgets/navigation/.
However, I'd would rather set an id attribute for one of the pages, then maybe define some data attribute in the link to tell jQuery mobile where to go. Possible?
I'd also like to specify what kind of transition effect to use.
You can use standard anchor links, just give an id to your page and set the transition via the data attribute
Link to Page 2

External link routes in rails

Simple enough. When were making a typical restful rails app we would keep all routes inside of our applicaiton. Very rarely would a path link to an external path. But if we were to do it, I'm wondering what the best way is.
A typical matching of home
match "home"=>"appcontroller#home"
If we were matching an external url to a variable of path. We might do something like the below?
First method
Routes.rb
match "external"=>"http:/www.google.ie"
Then in our html.erb
<%= link_to 'Google', external_path %>
Note this is not actually a legal way of doing things but something similar may exist. It seems very close to the current way of defining paths in rails but with an external landing.
Second method
Something that I've seen done elsewhere is to create a global variable for the external URL and use it in the link. EG.
in environment.rb or production.rb or whatever
#ext_path="http:/www.google.ie"
Then in our html.erb
<%= link_to 'Google', #ext_path %>
So to recap. Whats the best way to use external URLS in rails. Paths? Variables? Other?
Any input appreciated
I would have kept external links only in views. Because this links are not related to the any kind of logic of the application, and it's just an UI elements.
So, this way seems to me the best:
<%= link_to 'Google', "http://google.ie" %>
If you need to use this element many times, maybe it makes sense to bring this code into the helper, for example:
def search_engine_link
link_to 'Google', "http://google.ie"
end
And I really think that is's not very good place to introduce a more complex logic.
I'd probably use application-wide helpers wrapped around constants.
Helpers because I'd rather not see constants in templates, constants for environment-specific values.
I might use a hash to store the URLs: this keeps them more-tightly-coupled, and would allow environment-wide defaults, overriding per-environment as necessary.
The helpers could take symbols, or be generated from the hash keys, to generate xxx_path methods as happens with routes in routes.rb.
I found myself needing to do this because I had a link to an external site which I intended to make local in future.
In Rails 4 you can add this to config/routes.rb:
get '/contact',
to: redirect('http://www.example.com/contact.php'),
as: 'contact_us'
Which at the price of an extra redirect, lets you write:
<%= link_to "Contact us", contact_us_path %>
301-redirects
A note about use of redirect(...) in the routes file. Redirect generates a 301-redirect, which is a specific thing, Google describes it as...
If you need to change the URL of a page as it is shown in search
engine results, we recommend that you use a server-side 301 redirect.
This is the best way to ensure that users and search engines are
directed to the correct page. The 301 status code means that a page
has permanently moved to a new location.
So, using a 301-redirect in your routes for links to external websites may work, which it does, but the status information that is carried with that redirect is not correct.
If you've moved pages in your web app and want to tell the Google index (and everyone else) about it, then by all means use the redirect() to assure Google updates the index, but it's not optimal to use for standard external links out of your web app.

How Does Rails 3's "data-method='delete'" Degrade Gracefully?

Rails 3 does some cool stuff to make Javascript unobtrusive, so they've done things like this:
= link_to "Logout", user_session_path, :method => :delete
..converts to
Logout
But it just occurred to me.. When I turn off javascript the method isn't DELETE anymore, it's GET as expected. So are there plans to, or is there some way to, allow these data- attributes to degrade gracefully, so that link still is a DELETE request?
The change they made in Rails 3 with these data- attributes wasn't about graceful degradation, it was about unobtrusive JavaScript.
In Rails 2, specifying :method => :delete on a link would generate a whole bunch of inline JavaScript that would create a form with a hidden input and then submit the form. That was the same as it is now: turn off JavaScript and it defaults to a GET request. As such, supporting the case of no JavaScript is the same as it was before.
One option is to use a form/button instead of a link so you can include the method as a hidden field, much like the Rails 2 JavaScript does. Another option is to have the GET version take you to an intermediate page which in turn has the form/button.
The benefit of the new approach is that it's unobtrusive. The JavaScript for changing the HTTP verb exists in an external file and uses the data- attributes to determine which elements it should be attached to.
Rather than using the link_to method -- which would require you use JavaScript to ensure that the HTTP method is DELETE -- use the button_to method, which will create a form with a hidden input element which tells Rails to treat the HTTP method as DELETE rather than POST. If necessary, you can then use CSS to style the button in the form so that it looks like a link.
The only chance you have is define a form. A link can't be a POST with _method="delete" without Javascript or by form.
It is not possible without javascript.
I make a small jQuery plugin for converting data-method link attribute to pseudo hidden forms (used in laravel project for example).
If you want to use it : https://github.com/Ifnot/RestfulizerJs

Using ActionLink and specifying the DELETE Constraint

I'm using RestfulRoutes written by Steve Hodgkiss. So far so good but I do have a question. I have a Session Controller that I want to use to destroy a users session when they click the logout button. How can I do this with and action link?
So far I have the following.
<%= Html.ActionLink("Logout", "Destroy", "Session") %>
I need to hit the following route.
Session/{id} and have a DELETE constraint.
I hope this makes sense.
Mike
An anchor tag in HTML can only ever issue a GET, it cannot issue any other verbs (unless you intervene with JavaScript).

Resources