Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an app that scrapes data from a webpage. This scraping takes about 5-10 minutes and so it is not a very user-friendly experience. I would like for a user to be able to set up and schedule a time for the page to be scraped and then once finished send an email them alerting them.
So is there a way to set up rufus-scheduler to take user input into account? If not, how should I go about this?
Homework question, grrr. http://www.catb.org/~esr/faqs/smart-questions.html#idp54052224
Let's say you have a form that vaguely looks like:
<form action="/stupid/" method="POST">
<input type="text" name="schedule" />
</form>
it's POSTing to the StupidController:
class StupidController < ApplicationController
# POST requests for /stupid/ come here
#
def create
# expects something like "5m" or "10d"
s = params[:schedule]
#job_id =
Rufus::Scheduler.singleton.in(s) do
# do the job...
end
end
end
The controller extracts the user-chosen schedule from the form data and then schedules a job.
If that answer goes above your head like the ones at Can I schedule controller methods in rails using rufus-scheduler? then you should really invest time learning to learn instead of asking people to do your work.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 20 days ago.
Improve this question
i'm having trouble with my rails aplication.
i have a form who should submit some fields to be search to /cities/search/<params_here>.
but, when i submit form, the routing goes to wrong action, and perform a set_city funcion... (??? why this happens ??? )
if the route exists and be declared before the other routes generates from :resources, that should't work?
It seems that there are two problems with the above code.
1.The URL in the form_with:
The URL ideally should be url: "cities/search",.
Since you are using form_with, the values will be available in the form of query params.
This is where our 2nd change comes in.
2.The route that you've set:
It should be get '/cities/search', to: 'cities#search', and as mentioned above, the form fields and their values will be available in the query params.
In the cities_controller's search action, you'd get the params by using params[:query] and params[:search].
Please check form helpers once so that you get a clear idea of it's working.
I have not tested this, so let me know if this helps and if there are any other issues after the above changes.
Also, it is good practice to post the code in the questions in text format rather than images. Ref. this link to understand why.
You can use a collection route without needing to think about route priority:
resources :cities do
get 'search', on: :collection
end
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I would like to call to my customer mobile numbers from my web application browser itself(head phones with mike) as live(RTC) and should able to record audio also.
Is it possible scenario with twilio api?
I'm a Twilio developer evangelist and I think I can help here.
You can indeed do all of that. We have a quickstart guide on the Twilio website that gets you set up with making phone calls from your browser that you can follow to get 90% of the way there. You may need to adapt the example from Sinatra to Rails (as your question is tagged).
In order to record the calls, there is only one thing you'd need to change from the example TwiML when making an outgoing call. You just need to add in :record => true to the parameters you pass to the Dial verb. Below is the example code with the addition of the record parameter.
post '/voice' do
number = params[:PhoneNumber]
response = Twilio::TwiML::Response.new do |r|
# Should be your Twilio Number or a verified Caller ID
r.Dial :callerId => caller_id, :record => true do |d|
# Test to see if the PhoneNumber is a number, or a Client ID. In
# this case, we detect a Client ID by the presence of non-numbers
# in the PhoneNumber parameter.
if /^[\d\+\-\(\) ]+$/.match(number)
d.Number(CGI::escapeHTML number)
else
d.Client default_client
end
end
end
response.text
end
Once you have made the recording, you can then then get hold of your recordings through the REST API like so:
require 'twilio-ruby'
client = Twilio::REST::Client.new ACCOUNT_SID, ACCOUNT_AUTH_TOKEN
recordings = client.account.recordings.list
There's more information on the recordings API resource in the REST documentation
So, take a look at the quickstart guide and let me know if this helps.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Currently i am learning rails so i'm sorry for my bad knowledge and probably for a silly question. :)
I created a simple index page in a controller called pages.
Inside index.html.erb i have a form where a user can sign up for a newsletter which is stored in #newsletter and i also have a variable called #title where title of the page is stored in database.
I want to create an administration panel available only for the owner of the website where he can access and display all newsletters, also i would like the owner to have the possibility to change the title.
Can i achieve that if i create another controller ?
What do you recommend, to keep the same controller for admin and pages or create a new controller for admin area ?
Also, one more question, when do i need to create a model? Everytime when i need a new table in database, isn't ?
I should have a single model for every controller or i can have more models and one controller?
Is there any connection between pages and the newsletter?
If newsletter is not in a relationship with pages, then you can place the newsletter form in a partial and render it where you want.
This a possible db schema for you:
'Page.rb' model with the fields that you need like: title:string, slug:string (i prefer friendly_id), body:text...
'NewsletterEmail.rb' model with the fields that you need: email_address:string, name:string (optional), subscription_status:boolean (to track un-subscription)...
Every model needs a controller for you to track in admin.
For the front-end, you must setup a route like 'match: "/:slug" => "public#page"', where you can catch your page like #page = Page.friendly.find(:slug) and then assign the #title variable #title = #page.title.
Hope this helps you.
For your admin controllers pages_controller.rb and newsletter_emails_controller.rb you can use a before_action :check_admin_rights and define in application_controller.rb
def check_admin_rights
authenticate_or_request_with_http_basic('Administration') do |username, password|
username == 'admin' && password == 'password'
end
end
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here's what I need to achieve :
A user visit the login page. Upon the first visit, the browser will record his IP and data from a cookie (cookies[:user]) and send it to the server. The server then check whether the IP or the user is in the block list and if the user is in the block list, it will automatically redirect the use to a blocked page!
Can we do this in Rails and how?
This could be done quite easily, take this for example
def new
if cookies[:userid].present?
if User.find(cookies[:userid]).userblocked == true
redirect_to "/block_page"
end
elsif #failedtries == max_tries
User.find(cookies[:userid]).userblocked = true
cookies[:userid] = #id_entered
end
end
Where 'userblocked' is a true or false value in your database. This method is full proof against people who can freely change their IP. However if your heart is set on using IP's then this is an example:
def new
if cookies[:blocked].present?
redirect_to "/block_page"
else
if BlockedIps.find_by_ip(request.remote_ip).blocked == true
redirect_to "/block_page"
elsif #failedtries == max_tries
BlockedIps.find_by_ip(request.remote_ip).blocked = true
cookies[:blocked] = true
end
end
end
Something like this could be what you are after. Give it a try, I haven't tested the code. This is just from my head at this moment in time, but it should do what you want, providing you have some sort of system to store the IP in a database when the max tries have reached.
Explanation:
The way it works is that it checks to see if the blocked cookie is present, if so it redirects to the block page, if not it checks to see if the users IP address is in the block list of IP's. If the IP is present and the block column is set to true then the user will be redirected to the block page, else the system checks how many tries they have made and if it is equal to the max tries it adds the users IP to the database and creates a cookie on their PC which can be checked on next visit.
You can make a method for checking the IP address and authenticating then use before_action :yourmethod on top of your controllers. before_action runs before any other action in your controller so it is a good way to authenticate before showing any views.
You can use Conner Stephen McCabe solution above or take a gem for this purpose.
User can easily clear the cookie, so it's not safe anyway.
Example: https://github.com/kickstarter/rack-attack
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
For example: How objects are called from controller on view?
Which methods, techniques are running in background?
Please help.
Thanks in advance.
you should check these out
http://railscasts.com/episodes/397-action-view-walkthrough
http://railscasts.com/episodes/395-action-controller-walkthrough
http://railscasts.com/episodes/319-rails-middleware-walkthrough
These screencasts will show you what happens in the background.
This question is like how components in airplane interact with each other so plane flies?
Anyway, when you call any action at any url, Rails will try to match it with routes.
get 'posts', to: "posts#index"
will match /posts routes and will look through your controllers and start PostsController.action(:index).
After that, while executing index action of your controller it will initialize instance variable #posts which is usually Post.all.
And the last thing, PostsController will call renderer, passing action, all instance variables, sessions and url-params. ActionView Renderer will initialize the instance of view with proper template app/views/posts/index.html.erb, put variables there and send page to user.