I have a PagesController that handles all the static pages of my application. It has a method called join_session which renders the view pages/page_a.html.erb. This works.
My problem: I'm writing a method that renders the last viewed page when the user logs back in (using the Devise hook after_database_authentication in the User model).
Can I call the PagesController join_session method from the User model?
[I couldn't figure out how to do that, so I tried a hacky workaround: writing and calling a class method in PagesController, and rendering the view there, but since the name of the new method did not match the name of the controller method in the route, the variables in the view could not be resolved.]
My route:
get 'pages/page_a', to: 'pages#join_session'
Help would be much appreciated! :)
I think there is a pretty standard way to do this in your ApplicationController, you should check out this guide by Devise.
https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in
I hope this helps. If you have any questions let me know.
Related
I am quite new to rails but i have searched a lot how to do this but it doesnt seem to work for me. Im trying to create a new view called request for my model called steppy_steps, so i created a new file in the views directory called request.html.rb, added this to my routes, match '/request' => 'pages#request', also tried get "steppy_tests/home", and lastly added (def request, end), to my Steppy_Tests_Controller.rb but when i check the url it gives me an error:Couldn't find SteppyTest with id=home
I cant figure out what to do any help would be great! Thanks in advance.
You should read up on the MVC programming pattern (which is what Rails is based on)
In order to make a view, you need to have the controller and model aspects in place too. I think you're doing this already, but to help you understand more, I'll outline below:
Views : Controller Actions
If you want to show a view from the steppy_steps controller, you need to first have a controller action set up to handle the request. You'd normally use a self-named controller for this (controller name steppy_steps), and have various actions for that
In your routes, you'll then "capture" the request to your steppy_steps controller like this:
#config/routes.rb
resources :steppy_steps
This will create a set of RESTful routes, which you can then translate into your controller, like this:
#app/controllers/steppy_steps_controller.rb
def index
#Index code
end
def show
#Show code
end
This will allow you to create a views directory which can contain the views for /views/steppy_steps/show.html.erb and /views/steppy_steps/index.html.erb
Routes Are Super Important
The error you're getting is caused by you sending /home to your view
The problem here is that if you're using a route which has an in-built id param (the show action routes have this), then Rails will look for the extra params after the URL
To fix this, you'll have to provide more of your code, but I also believe you'd be better understanding the fundamentals of Rails a little more
Adding Routes
You can add routes & views as you wish
If you're looking to add a requests route / view, I'd do this:
#config/routes.rb
resources :steppy_steps do
collection do
get :requests
end
end
This will allow you to create /steppy_steps/requests
I currently have a Shop's Controller:
http://localhost:3000/shops/testing
and i want to add an about and policy page to the existing shop
http://localhost:3000/shops/testing/about
http://localhost:3000/shops/testing/policy
Do i have to generate a seperate model or views or add to the Controller ?
This Question might sound very stupid, but i'm new to rails and cant get over that Problem.
If someone could enlighten me.
Thank you
If your whole page is a shop, then there's no sense in making a single shop controller that contains every action of the shop. Instead, create controllers for the shops "parts". Normally, you would have a separate controller for static pages like about or policy.
Rails will, by default, search for a file that has the same name as the action of the controller under the folder with the same name as the controller and load it, after the code in the controller is executed.
So, if your controller is Shop, and the action is policy, just add a policy.html.erb file under the views/shop folder. Finally, add this to the routes.rb file:
get 'shop/test/about', to: 'shop#about'
Consider the getting started guide, which covers all this.
Hi I'm a beginner of rails and I'm not good at English. so if there is some total nonsense please understand..
I'm trying to record loading speed and page duration in every pages.
I made a database "pages" and method "savepage" in my "Page" model.
To save in every page I put "savepage" method in application controller.
Page.rb
def self.savepage
.
.
.
end
application_controller.rb
before_filter :dosave
def dosave
Page.savepage
end
these kind of format..
My question is
1. am I doing correct? using before_filter to do save in very first of loading process?
2. to save after loading all the contents in a page what should I use?
3. to save after user leave this page what should I use?
I saw before_destroy and after_filter, but I can't find what it is... what filter means.... what action means destroy....
thank you in advance!
before_filter is the first thing which loads before giving request to controller.But your need is completely different . Fundamentally filter are used boolean checking.If certain method is true,it will run otherwise it may not. This filter are further extended and we put code into that filters.(And Even sometimes it is consider as best practice) .
Now, before_filter :dosave might be right but is it not true way of knowing page(UI) loading process. I suggest you to use javascript call or use some manually created helper methods and place it into view .erb files.
May be this will interest you
https://github.com/grosser/record_activities
Log user activities in ROR
what action means ?
Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straightforward as possible.
Source : http://guides.rubyonrails.org/action_controller_overview.html
I highly suggest you to read above documentation. It is very necessary for you and it covers topic which you asked here.`
And one more thing,
what is action destroy ?
This is simply an action method just like new. Since, rails follow Convention over configuration ( and its developer too) so they put code which do some delete destroy or some destruction. This make thing simple,otherwise more configuration will require which is against rails policy.
Sorry, I have a Rails newbie question. In my Rails application, how can I call a method defined in my controller from the view? For example, let's say I wrote a custom method that retrieves stock information. In my view, I want to have a regular HTML button, that upon clicking, will call my custom method and populate the stock results.
How is that done? I've looked around and couldn't find a straightforward way to do this. But I'm sure I'm missing something here.
Edit: I took the question by its title which wasn't what the question was. The answer to the title of your question is at the bottom
What you want is an ajax request, which is a separate controller action. You'll need:
javascript to request a route and populate its DOM object when the button is clicked
an action that returns whatever the ajax request was asking for
There are many ways to do this, but if you search for "howto rails ajax" you'll find a gazillion tutorials to help you on your way. One way that I like is called pjax: https://github.com/rails/pjax_rails
Old answer...
Declare a helper_method, like so:
In your controlller:
private
def find_stock
...
end
helper_method :find_stock
In your view:
<% find_stock.each do |stock| -%>
Documentation: http://api.rubyonrails.org/classes/AbstractController/Helpers/ClassMethods.html#method-i-helper_method
You can check the documentation for the button_to view helper
This is one way, of course, you could use the remote option to do this via an AJAX request.
It is probably understood that I am pretty new to Rails without me even mentioning it :)
So I have a static controller which is working. Here is the route:
match "/pages/:page" => "pages#team"
And I am able to confirm that this route is working because this controller code outputs things to the console:
def team
print("Output some test code to see if system gets here\n")
end
What I am trying to do is have the controller redirect to a view page that I made. My view page is: team.html.rb and it is located at /app/views/pages/team.html.rb
How can I make the controller redirect there?
Thanks!
Make sure you have an action in pages for team
#in PagesController
def team
end
Then rename your file to:
team.html.erb
Just an FYI, you may want to checkout highvoltage from thoughbot for handling static pages:
https://github.com/thoughtbot/high_voltage