New to Rails here. I got a short script in Rails at detectmobilebrowsers.com to check and redirect mobile browsers. It looks like:
def redirect_mobile(url = "http://detectmobilebrowser.com/mobile")
redirect_to url .....
.....
(request.user_agent[0..3])
end
It looks like this is one big function. Where should I put this in the typical Rails directory structure (app with controller, helpers, models subdirectories / components / config / etc) so that when a user accesses the front page (index), they'll get redirected to a certain mobile address?
Thanks!
You can set this up as a before_filter on your ApplicationController. This controller is inherited by all of your other controllers, so its filters run on every request.
Under app/controllers/application_controller.rb,
class ApplicationController < ActionController::Base
before_filter :redirect_mobile
private
def redirect_mobile(...)
# ...
end
end
Though, I highly suggest that you modify that script to do smarter redirects, otherwise a mobile user visiting http://www.example.com/products/123?color=green will simply be redirected to the mobile homepage, rather than the mobile version of product 123 (in green!).
Related
I'm real beginner in Rails.
I created app/services/xclass.rb class with some_method inside.
I need to execute some_method using url.
For example, I want run this method when I execute in my browser url - http://application.com/notifications/send
I think it could be done through controller (notifications_controller) but how to do it?
I created only controller, with no model, just for launching some_method.
first, create a route:
get "notifications/send" => "notifications#some_action", :as => "send_notification"
Then create a controller action in your controller (ie. NotificationsController):
def some_action
Xclass.some_method # run the method you want
redirect_to root_path # redirect or whatever you want here
end
Now you can either visit the path http://your_app.com/notifications/send, or link to is using 'send_notifications_path' url helper in rails.
That should do it
Since you're a beginner, let me give you some ideas
MVC
Firstly, you need to appreciate that Rails is an MVC (model view controller) framework:
In short, this means that every time you send a "request" to Rails, it will be "routed" to the specific controller action which corresponds with that route.
This means that when you ask about how to fire a "class method", you're going to have to work within the confines of the MVC programming pattern. Here's how:
#config/routes.rb
resources :notifications do
get :send, on: :collection #=> domain.com/notifications/send
end
#app/controllers/notifications_controller.rb
class NotificationsController < ApplicationController
def send
#call your class method here
YourModel.class_method
end
end
#app/lib/your_model.rb
class YourModel
def self.class_method
#do something here
end
end
--
Rails
This is further supported by the fact that Rails is just a framework - in fact it's a gem (a great one) which runs on top of Ruby.
This means that even though some of the ways in which Rails works might seem somewhat alien to begin with, you have to remember that it basically just captures "requests" fed to it by a web sever, processing them with connectivity to the database etc.
The issue here is that as you're sending the request over HTTP, you have to work within the constraints of this protocol (specifically that it's stateless), and with Rails. As mentioned, Rails is MVC-based, which means that every request will be routed to your controller, which is why you have to create the corresponding route & controller action to handle it
If you use the code above (tweaked to your app), it should work for you
I'm doing some proof of concepts with Rails. I created a Customer, and apply to it DEVISE.
Everythig works fine, but now i'm trying to logging with a customer and enter directly into his scope.
localhost:3000/customers/1
After this hide the /customers/1 with some word, for example: myProfile.
I'm trying to do the first part with
module ApplicationHelper
protected
def after_sign_in_path_for(resource)
#path to redirect
end
end
I was trying with definitions from routes.rb
redirect_to(customers_path)
or something like:
#customer= Customer.find_by_email(current_customer.email)
redirect_to(customer_path(#customer))
But nothing is working yet.
I'm not sure how to send messages to the console of the server (like in Java with some System.out.println) to check the contents...
Thanks a lot!
Situation: rails 3.2 app with a demo period, after which users must start paying for the service.
Question: If a user does not add a payment method, or does not choose a payment plan, what is the recommended way of restricting user access to the 'paid' part of the web app?
I need something that sorts users as follows:
if user.admin? || user.in_demo || user.has_all_payment_data
# carry on
elsif user.should_add_payment_method
# send them to add payment method page
elsif user.should_choose_plan
# send them to add plan
else
# redirect to home page or whatever
end
I've started off with a before_filter on the application controller that checks the payment status of the user on every request and redirects them accordingly (skipping this in places like the homepage/profile editing etc.), but I'm thinking there must be a better way, as it's rapidly getting too complicated and it just feels wrong having all that complexity in the application controller. I've been looking at user roles libraries like cancan but I can't find anything that fits.
There is a post by Jonas Nicklas (creator of Capybara and CarrierWave) in which he explains in some detail how to take a simpler approach than CanCan's. His approach is based on an additional plain Ruby class for each model you want to create authorization rules for.
Simple authorization in Ruby on Rails apps (Elabs blog)
They have offloaded that solution into a gem named Pundit, but it really seems simple enough to be able to implement from scratch.
Pundit gem (GitHub)
I would suggest a before_filter in the application controller, then using skip_filter in individual controllers to bypass it for actions that non-paid users can access, e.g:
class ApplicationController < ActionController::Base
before_filter :check_payment
...
end
class UserController < ApplicationController
skip_filter :check_payment, :only => [:login, :logout, ...]
...
end
This keeps the access contained to the relevant controllers, rather than needing an increasingly large :except => ... on the filter itself.
So there's a bit of code that I want to execute on every request that is made to my rails application and found through googling that I can do it using before_filter. Buy it doesn't seem to run when I go to the initial page, which is localhost:3000. Anyone know why it doesn't run?
I'm using Ruby on Rails 3, WebBrick server on Lubuntu Linux.
class ApplicationController < ActionController::Base
before_filter :run
private
def run
logger.debug "run? I'm too tired"
end
end
If your initial page is a 'static' page served from the public directory this code will not be run. To fix, make the page render as the result of some controller's action that inherits from ApplicationController.
I am a new Rails Developer
My application_controller is:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :sshow
def sshow
puts "==========================="
puts YAML::dump(request.subdomains)
end
end
now when I put kausik.localhost:3000 in my browser address bar it
returns blank Array [] instead ['kausik'] .
Also I rewrite etc/host file for this subdomain.
As #injekt said in the comments, you can't work with subdomains using localhost. The simplest alternative is to use the lvh.me domain which points all subdomains to 127.0.0.1. To do this, simply start your rails server and go to kausik.lvh.me:3000.
Also, if you simply need the first subdomain, you can use request.subdomain, which in this case would return "kausik".
There's a very nice RailsCast about subdomains in Rails.