I'm trying to work out how to redirect mobile users to other views in my rails app, but I'm missing something as it's not loading the mobile view from my device
in application_controller.rb, I added:
def check_for_mobile
session[:mobile_override] = params[:mobile] if params[:mobile]
prepare_for_mobile if mobile_device?
end
def prepare_for_mobile
prepend_view_path Rails.root + 'app' + 'views_mobile'
end
def mobile_device?
if session[:mobile_override]
session[:mobile_override] == "1"
else
# Season this regexp to taste. I prefer to treat iPad as non-mobile.
(request.user_agent =~ /(iPhone|iPod|Android|webOS|Mobile)/) && (request.user_agent !~ /iPad/)
end
end
helper_method :mobile_device?
then I have a file app/views/views_mobile/guidelines/index.html.erb
When I go to the index page on my iPhone it doesn't load the mobile index view - I'm not sure which part I'm missing...
Redirection for this kind of issue is a bad idea. Try responsive design. I would start with twitter bootstrap. This will give you a scaffold system to start with that will adjust to different screen sizes.
Also this is not uniquely a ruby on rails issue. This is a UI design issue.
Here's some good ideas.
Dont duplicate your views. it will just be harder to maintain down the road.
Use css media queries in your css to adjust your styles.
Try not to make two sites but one site that can bend and flex as it needs to.
All that being said I did not answer your specific question but instead tried to show you the way your going about it is wrong so I will understand if you dont mark this as correct.
Responsive resources.
http://twitter.github.io/bootstrap/
http://neat.bourbon.io/
http://www.abookapart.com/products/responsive-web-design
http://www.w3.org/TR/css3-mediaqueries/
Unless you left it out of your code snippet, I don't see where you're calling you're calling your check_for_mobile method. You probably need to add a before_filter to application.rb like:
before_filter :check_for_mobile
I also suggest you check out Ryan Bates Railscast on how to incorporate mobile-specific views into your Rails app. His strategy is similar to what you're trying to achieve, but instead of using a subfolder for your views he solves the same problem very elegantly by creating a 'mobile' MIME type. Check it out here:
http://railscasts.com/episodes/199-mobile-devices?view=asciicast
Related
I've got an app that's using JQueryMobile and it's using the awesome ActiveAdmin extensively as well. While I love the ease and simplicity of the ActiveAdmin interface, I'd really like consistency with the rest of my app.
Is it possible (i.e. using standard ActiveAdmin and not modifying its sources) to re-skin ActiveAdmin to use the JQuery-Mobile look and feel?
Its very possible to reskin ActiveAdmin, though it would be a bit of a job to do, and there would likely be quite a number of things that can't perfectly be built to match a mobile presentation, especially if you don't want to get into overriding markup rendering.
You can always simply start adding styles of your own to the active_admin.css file that is generated for you. If you'd like to start without any of ActiveAdmin's styles at all, you can comment out the two sass imports in that css file:
#import "active_admin/mixins";
#import "active_admin/base";
Or at least just the base file. It may be intriguing to you in itself, or informative about the organization of the markup, to view your current admin pages without the base css, or with css turned off in your browser altogether. From that vantage point, you could begin to think through how the bare markup could be restyled to match a mobile presentation.
Does anybody know how to upload a document to later show in a Rails application (as text)? Is Paperclip the right gem to do this? If it is how? (I have uploaded images before with Paperclip).
I like Paperclip. It seems well documented, and has worked well for everything I have needed. (I don't personally know any of them, but the clever folks at Thoughbot have created some pretty useful stuff, for which I feel indebted to them).
Obviously, you need to add Paperclip to your Gemfile, and (if you are using bundler) do your bundle install
Add to your model
has_attached_file :aFile
Add to you controller something to catch whatever you name it in your view (probably in your create and update methods)
#profile.aFile = params[:profile][:aFile]
Probably should check for its existence, if it is a required param
if params[:profile][:aFile].blank?
redirect_to #profile
else
render :action => 'do_something_interesting_with_file'
end
And that's about it. Don't forget your config entries. For example, if you are using some kind of post-processing on the file
Paperclip.options[:command_path] = "/opt/local/bin/"
I found this to be extraordinarily helpful
RailsCast by Ryan Bates
I want to stream a large amount of text from a controller to a view in real time and was pointed to using the following code as an example of how to handle the streaming
def home
self.response_body = proc {|resp, out|
10.times do |x|
out.write "count = #{x}"
sleep 1
end
}
Now this code works in so much the content streams to a browser with 1 line appearing every second, however it doesn’t use the home.html.erb view I created instead it only renders a blank page with the streaming data on it
I’ve tried to embed it using various bits of erb code but cant get it to work and I’ve had a hunt around the web and cant see any clue as how to do this
Can anyone help?
I'm using Rails 3.0.7 and ruby 1.9.2, in dev I'm using unicorn as the rails server which handles the streaming
Cheers
Mike
If you set the response_body in the controller it will not render the view. So I'm afraid you have to pick one or the other. In fact, I've received an exception when I tried to send a response to the client via two different methods. In my case it was send_data and self.response_body = ...
I found a tutorial here https://github.com/plataformatec/devise/wiki/How-To:-Migrate-from-restful_authentication-to-Devise-, however it seems like it is missing pieces of what to do, for example the restful_authentication plugin is still there...how do I remove it? Then how does Devise know what to get for my App...I've tried it but it just keeps breaking my App.
Erase everything in the user.rb controller (or your controller)
Make Sure all of Restful_Authenication is gone (there is quite a bit of includes that may be presnet)
Make sure devise_for :users is present in the routes.rb file
Look for method errors and replace them back in the User.rb controller.
Think that fixed it.
It gives me the creeps,i'm done, i need some help here, i reverted multiple times back but i can't find the error.
Simple controller (customers),a simple form for adding a customer via :remote => true and the controller does respond_to do |format| { format.js } . Works fine, renders my create.js.rjs template.
I work for a few hours without making any javascript changes or changes to my controllers or authorization etc.. and when i try it again it's not working anymore.
What i mean with not working: Controller gets called, record saved, all partials rendered. But no javascript evaluated, not even a simple alert(1) at the beginning of the file.
I tried with different prototype.js versions and different rails.js versions, but nothing helped. I hope someone has a clue about this or already experienced this.
It's not that i don't want to post code. But it won't help. Its basic code that works and, after some changes where i don't know what i really changed (some locales here, some css there, html templates from a completely different controller a bit..)..
Currently developing with: ruby 1.9.2, rails 3.0.3, prototype 1.7 RC3, rails.js from github.
SOLVED
How stupid, I missed the part where the template naming changed. My application templace was named "application.rhtml". It worked until now. As it stopped to work, I changed it to "application.html.erb" and now it's working.