How rails component interact with each other in background? [closed] - ruby-on-rails

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.

Related

Route perform wrong action on submit form_with [closed]

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

Admin panel not working in ruby on rails [closed]

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

Can I allow users to schedule tasks with rufus-scheduler? [closed]

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.

RESTful API best practices, update vs custom action [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm implementing a RESTfull API to talk to AWS RDS, security_groups resource supports the typical CRUD verbs. When it comes to "authorize" and "revoke" i'm not sure what's the best practice, which one do you think is best?
Custom action, params in url
PUT agifog:3000/rds/security_groups/:security_group/authorize?ec2name='default'&ec2owner='0123456789'
Custom action, passing params
PUT agifog:3000/rds/security_groups/:security_group/authorize
{
"ec2name": "default"
"ec2owner": "0123456789"
}
Standard update
PUT agifog:3000/rds/security_groups/:security_group
{
"operation": "authorize"
"ec2name": "default"
"ec2owner": "0123456789"
}
PUT does not mean "update" any more than POST means "insert". PUT means "put this here".
RESTful practises revolve around treating your URLs as resources, entities which have some meaning in your domain, which you perform actions against (the verb of the HTTP request).
What you could do is consider the security group to be the resource on which you are acting and PUT users into the group or DELETE them from the group:
PUT agifog:3000/rds/security_groups/:security_group/default
{
"ec2owner": "0123456789"
}
DELETE agifog:3000/rds/security_groups/:security_group/default
These could then correspond to authorize and revoke actions, plus makes it easy to see how a GET on the group could produce a list of all the users currently in the group.
The second seems the most RESTful. You've got a resource (security group) and a custom action (authorize) that will respond to your request's verb (PUT).
PUT agifog:3000/rds/security_groups/:security_group/authorize
{
"ec2name": "default"
"ec2owner": "0123456789"
}
and similarly:
PUT agifog:3000/rds/security_groups/:security_group/revoke
(NOTE: I'd probably prefer a POST for the above if it will be generating a session or some other authentication data/token.)
For comparison, if you were interested in updating the attributes of that resource, you'd want to do something like:
PUT agifog:3000/rds/security_groups/:security_group
{
"some_attr1": "some_val"
"some_other_attr": "some_val"
}
In which case the PUT implies that it is an UPDATE to this resource.

Controller action is called twice [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I've noticed, that "index" action of my controller is called twice.
The action has following structure:
def index
if params[:tags].nil?
# [fork #1] just return the whole collection of model
#items = Item.all
else
# [fork #2] filter items by tags
#items = Item.select_by_tags params[:tags]
end
# Another operations with #items
# The result will be incorrect, because, when tags in params are specified,
# controller action will be first executed for fork #2, and then for fork #1.
# In view, i get #items from fork #2 and result of THIS piece of code for fork #1
end
On the page i have links with URLs, like "/items?tags=tag1, tag2", clicking on them i get "index" action, called twice
I have no idea, why this happens ...
I've figured it out.
The reason of such a behavior was a JavaScript code, being called after page load. This code implements deep-linking for some parts of the page.
Be careful about this.
Thanks.
I faced similar issue and in our case it was an offending pjax code with 6s of timeout resulting in one extra request (i.e. one extra regular HTML request) if pjax didn't complete in 6s.
Another thing to check is whether you have PostCSS listed as a dependency in your package.json. If you do, and you don't have a postcss.config.json file, fix that. The most basic file appears to be
module.exports = {
plugins: []
}
Restart your server, reload your page, and check the server log. There should be only one request being reported as processed.
Very late to the party, but hope this helps someone. I was temporarily stumped in a trivial Rails 6.1.4.1 app seeing the log.

Resources