I have watched railscasts about unobtrusive javascript and read numerous tutorials, but two things about unobtrusive javascript in Rails3 still confuses me:
End-point for simple javascript (hiding some elements, adding CSS class, ...)
Passing arguments to JS
Let me clarify this with sample code. I want to make a link which fades out some element with id "sample":
link_to 'Fade sample', url, :remote => true
What should be the url so it can execute JS? Should it be new action in controller named for example 'javascript' so it can access JS in javascript.js.erb which contains:
$('#sample').fadeOut();
Also, second question about ujs is related with passing arguments to JS (timeout, for this example). I can write:
link_to 'Fade sample', url, :data-timeout => 1500, :remote => true
but don't know how to access data-timeout in javascript.
I am using Rails 3.0.5, JQuery 1.5.2 and jquery-ujs.
I would try to answer one by one.
url in link_to. It would be a url hash for any resource in the application. Lets suppose you have specified resources :foos in the routes.rb, it will create 6 urls for CRUD operation. You can pass these to the urls by the methods like new_foo_url, foo_url(#foo) or foos_url. Even you can pass a hash manually like {:controller=>:foos_controller, :action=>:index}, also you can pass multiple parameters to the hash like {:controller=>:foos_controller, :action=>:index, :param1=>"value1", :param2=>"value2"}. Also you can pass strings urls like foos/new, foos/1 etc
You can access custom tag attributes with jQuery very easily. for example you want to access anchor with id="link", you can $('#link').attr("data-timeout"). If you don't know what exactly the id is, but you know it have an attribute you can call $("a[data-timeout]").first().attr('data-timeout')
Related
I'm trying to use the erb template on my angular templates folder, but the functionality is limited, for example,
The <% link_to
is working
but devise methods or even raw('sdmdslkc') pops an error that the method is not found.
for example
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
work on views but not in angular templates (says destroy_user_session_path method not found)
Whats missing ? is it fixable?
let me see if i understand, u have some template with some tags that belongs to another language that is not angular, html, or js. if so then it is not fixable because angular sends an ajax to bring that html file and injects it to your page, alot after any server has rendered any part of the page.
my suggestion for a work around is to try and tell angular to take a server page, say it was with asp.net and i would like some server tags i would tell angular that the template is somepage.aspx while making sure that my page evetually returns plain html.
I'm writing an app that makes use of AngularJS, so the app is setup to route all requests to the main home page where angular takes over. All routes are then defined within an api scope which angular uses to retrieve the data. It's setup though, that if the user navigates to a page with a normal URL, then when it redirects to the home page, it maintains that URL which angular then uses to load the correct state.
What I now want to do, is be able to use URL helpers within rails to generate my URL's, but not have the generated URL's include the /api of the scope. Is there any way I can get around this?
routes.rb looks a bit like
scope "/api", shallow_path: "/api" do
... normal stuff here ...
end
And if I try using one of the helpers,
meeting_url(#meeting, subdomain: "test")
the url it generates is
http://test.domain.com/api/meetings/1
Is there a way I can have it strip the /api?
I don't believe there's a built-in way to do it.
But, you're in ruby, so there are plenty of ways to do what you want.
One way to go, since you're in your own app, is do monkey-patch String:
class String
def no_api
self.gsub(/\/api/, '')
end
end
meeting_url(#meeting, subdomain: 'test').no_api #=> http://test.domain.com/meetings/1
If you find that distasteful, you can define a method on ApplicationController or in a helper module:
def no_api(url)
url.gsub(/\/api/, '')
end
etc. etc. etc.
I was recently tasked with loading a portion of my Rails application within an iframe on another website. The relevant pages should be using a different layout file, but only if they're being rendered inside of the iframe. There was a solution proposed here Detect iframe request in a rails app that involved passing a query string parameter.
For example the requesting website could call my application through an iframe with the src of http://foo.com/bar?iframe=true. Then in our controller we could simply check:
def bar
render :template => "iframe" if params[:iframe]
end
This seems like a good solution, but sadly that only works for the initial request as the original query string is completely static. Assuming we have accessible links to other routes within the iframe is there any way of easily relaying the iframe=true request parameter to maintain the correct iframe layout without having to repeat code? Basically I would like to take the DRYest approach possible without breaking any existing functionality. I considered creating another link_to helper which included the logic to relay this parameter if it exists and replacing all of my link_to calls throughout my application; I was wondering if anybody had a better approach though.
I decided to tackle this problem using JavaScript and added the following to my haml layout file:
:javascript
for(i = 0; i< $('a').length; i++)
{
if($('a')[i].href.match(document.domain))
{
$('a')[i].href = $('a')[i].href + "?iframe=true";
}
}
This coupled with my server-side checks for the iframe param will ensure that the appropriate layout is loaded. I decided to only cater this functionality to users who enable JavaScript so it might not be the best solution. The only other problem with this approach lies in controller redirects and forms where I have to manually check for the iframe param and then forward it accordingly - not DRY at all, but I was at least able to put the logic into a controller method. If somebody knowns of a better solution please feel free to leave an answer.
I'm using Rails 3.1 here, and I've got the following code in my view:
<%= link_to "again!", main_pick_path,{:method => :post, :var => #var} %>
The idea is to create a link (not a button) which, when clicked, calls the pick action of the main controller, passing the value of #var in params via a POST request.
This code generates the following HTML in my browser:
a href="/main/pick" data-method="post" rel="nofollow" var="foo">again!</a>
However when I click the link I am still sending a GET request. Is this a limitation of my browser, Chrome? From a design standpoint, should I be using a GET request instead and putting the variable into the URL? Are hyperlinks simply incapable of using the POST method? Is there life after death?
Thanks in advance
You can only do this with AJAX or firing a FORM. The tag A cant do a POST "alone".
I have a nested resource, setup like this:
resources :chickens do
resources :eggs
end
The views for the EggsController are under views/eggs, but:
describe "eggs/index.html.erb" do
gives a "No route matches {:action => "create", :controller => "eggs"} error on calling render. How do I get RSpec to find the correct (nested) route for view specs?
As a side note, is there any way to specify a nested resource to the Rails 3 controller scaffold generator?
The test looks ok to me...
By any chance do you have a form on your eggs/index.html.erb for creating new eggs that might not yet be wired up correctly? It seems it may be trying to render the index view but failing because the view is trying build a route that doesn't exist? You'd want to make sure that the form is using the correct nested resource route. Does the view render when you load it up in the browser?