I am working on a Rails app that declares a :mobile format for iPhone and Android and serves up show.mobile.haml for mobile and show.html.haml for web (for example).
The mobile request format obviously uses application.mobile.haml and web uses application.html.haml - but both layouts are the same, only the views differ.
My question is - how do I use a single application layout for both the mobile and html request formats? Have dug through the Rails API documentation and can't seem to find an obvious solution.
You don't need to do anything. This happens automaticly when you add an extension to your urls. If you go to /controller/action.mobile the action.mobile.haml view will be rendered. If you go to /controller/action.html the action.html.haml view will be rendered.
This behaviour is managed by the format parameter (you can see this in your routes file). So /controller/action.mobile is the same as /controller/action?format=mobile.
Ofcourse, sometimes you'll want your actions to behave differently depending of the current format. This is supported be using the respond_to and respond_with method. More info about these can be found here.
Related
I'm actually making a rails app for a music band. And they recently asked for a music streamer to play music throughout the whole application.
As they're on bandcamp, I thought that I might as well do that via the iframes they provide, before building a javascript streaming feature in some time.
But, here's the issue : when you put an iframe in your application.html.erb, it's reloading itself everytime the user is loading a new page. Exactly as if the code wasn't in the layout, but on every pages instead.
So far I've tried some stuff, like putting the iframe in a partial and calling it via : render 'layouts/shared/music_widget', but the issue stay the same.
As I've found nothing on the web so far, I'm guessing I've missed something ( maybe I lack some knowledge in rails' basic magic )... so, I'd be glad if someone here could help me with this one.
Thanks !
That’s because when you reload, a completely new page is generated and downloaded by your browser. The whole HTML is replaced with every HTTP request. To achieve what you want, you’d have to look into asynchronous solutions and SPA’s (Single-page Applications), basically having only one page and replacing the content of it using AJAX.
I suggest using batman.js, a great library which makes it relatively easy to switch to AJAX page loading using Rails. A big advantage is that it was built with Rails in mind, and as such it couldn’t be more simple to integrate it with your current application. However it does require you to learn CoffeeScript.
Alternatives include AngularJS, Ember.js, Backbone.js, each of them having gems helping with Rails integration.
I am sure there are many more, but I listed the most popular choices. You could also create your own JavaScript to handle that. The easiest solution in such a case would be to have the big <div> containing everything but the iframe; bind to the click event of a elements with a special attribute set (for example data-ajax="true"), make an AJAX request to the URL specified in href, and replace the content of the big <div> with the response.
In any case, you’ll need to read more about Single-page Applications.
I am working on a similar project which requires the use of iframe to play music thoughout the website. For that I used two layouts, one is the application.html.erb and another one is called player.html.erb
Now Application .html.erb is the one which contains header, footer and the iframe. And the other layout does not contain any of these and is the one which is used to for the actions to be opened in the url.
I would like to convert an existing rails app with multiple models,controllers and views to a single page app (SPA). How can I render views for each model not as separate html pages, but as sections of the main page (say a div for each section), which could be navigated to by scrolling vertically? Is it possible to get the same user experience, I mean specifically vertical scrolling, in a standard MVC Rails app?
Well, to convert a standard rails app to a Single Page Application(SPA) you need to hook it to a MVC front - end framework. The html that was being rendered by the rails calls previously will now be fed into the front-end MVC framework which will render portions of a page instead of the complete page by making AJAX calls.
Nothing at all changes with the models and almost the whole of controller codes also stay untouched.
As a front-end MVC framework you can look into angular.js which is from the google stable of products or backbone.js which I personally find great. In fact there's a whole host of other frameworks ranging from heavy and full- featured like ember.js to minimal and necessary like handlebar.js
If you're looking for tutorials, tutsplus has a tutorial on backbone on rails that I know of. Hope this gets you started.
You have to render the views in the page you want to display ,there is noting to do with model and controller code ..
for this refer following link
http://guides.rubyonrails.org/layouts_and_rendering.html
It's perfectly possible, you should call your actions through javascript instead of html so that each actions return a portion of the page you want to modify instead of reloading the whole page.
Example, suppose you want to add a user to a list of user:
when you click the add button you make a ajax post to your controller.
this actions responds to the js format with a javascript file (controller_action.js.erb)
This js file will evaluate a partial template corresponding to a single line of your table (_user.html.erb), find your table and append the evaluated html to the table
Have a look at :
http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html
Specifically this section which answers your question with example :
http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#server-side-concerns
Try turbolinks gem
Your app will be similar to one page application with less efforts and less time.
I have a single form that I had created for up to date browsers, but have to also ensure I have a version that renders fine on an embedded IE6 (old call center), I'm looking for a way to tell MVC to send my IE6 friendly form when it loads in that browser..
All the documentation I read is mobile related and has me confused.
If you're using MVC3, I believe you're limited to checking the user-agent string in the controller method, and returning the specific view you want to be returned. Something like:
if(Request.UserAgent.Contains("MSIE 6.0"))
{
return View("IE6View");
}
else
{
return View();
}
Since you say you have a single form only, this might be the simplest direct solution.
If you're using MVC4, Display Modes is available, where convention is used to automatically select views based on conditions you outline in the Global.asax. Decent write-up for this approach at the link below. It's written for mobile-specific views, but could be easily applied to a different user-agent string condition.
http://msdn.microsoft.com/en-us/magazine/hh975347.aspx
I am loading a form onto a page via ajax in my Rails 3.2 app. These views are being picked up by Google and the urls just render blank pages. Should I somehow prevent access to these pages when attempting to view from outside my app (http referrer or something like that) or just use the robots.txt file?
Therefore my question is: What is the best thing to do when you have a view which is only ever loaded in via ajax. This is a programming question, as I am happy to code in whatever best practices to my controller files etc. However, it is also from the perspective of SEO and the performance of my app.
I don't want Google to index these pages - however - I don't know if preventing access directly is the best option (such as in this question: How deny access direct URL to my partial views? <- wrong environment, or this one https://stackoverflow.com/questions/11522307/prevent-direct-access-to-certain-urls-only-rails-app-can-load-them <- related but unanswered.)
Of course, I could define these pages in my robots.txt and stop them being indexed, however, maybe there is a better solution.
If you don't want Google to index any of your apps pages for what ever reason Ajax or not I suggest simply using a robots.txt file to Google not to index them.
I'm not aware of a way to block specific partials, Only whole pages.
This would be considered an SEO best practice.
Is there any reason you felt this may not have been the solution? UX? Google TOS?
What is the purpose of the rendering application class in Blackberry?
First, it is an Interface(#see http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/browser/field/RenderingApplication.html), it can allow you to implement HTML rendering in a variety of applications (#see http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/browser/field/BrowserContent.html)
I personally have used it to render RSS HTML content for display in a typical Field among other things.
It allows you to implement callback functionality to gather resources from the HTML (images, etc) and render them appropriately (if you desire).
This of course is the 5.0 and under version, as I have not been privileged to have a 6.0 user base so I cannot comment past 5.0 :)
edit: Oh yeah, it also allows you to handle HTTP events (redirects, etc)