I'm a programming noob, and learning Rails. I'm making a GTD webapp for fun, and I'd like to make a "done" section. In other words, I have a link that says "Done!" and I want that to move the current task into a done page, instead of index.html.erb. All I know about Rails is what was in the official beginner tutorial.
It sounds like you want to set a property like 'completed' on a task. There are a lot of things that have to happen for this to work
Your task model needs to have a property like 'completed'
Your controller needs to have an action to mark as completed (you can possibly just use the standard 'update' action..)
You need to have a route to that action
Your view needs to call that action
Related
I'm new to programming and need some help to create a basic timer in Ruby on Rails which can be started and stopped trough a button click in a view. For each stopped time a time-entry should get saved inside the database to the corresponding task.
I actually don't need code examples but some help where to start (code would be nice tho'.).
What the view looks like:
As I wrote, when the Start button got pressed, a time-entry (containing the start time as a UNIX timestamp) should be saved to the databse and belong to the current viewed task. When clicking Stop the current time, again, should get saved as UNIX timestamp. The next click on Start should create a new time-entry and so on.
I don't want to use any gems if possible.
Thanks in advance!
Honestly, there are many ways how to approach this. I suggest this (simpler) approach which should also teach you how everything works in Ruby on Rails.
Update it in the controller every time a button is pressed. Create new methods start_time and stop_time in your tasks_controller.rb where you update the time. Update the routes.rb file so you have the paths to the methods. In the view (show.html.erb in this case), create two buttons that redirect to the controller like so:
<%= link_to "Start", start_time_task_path %>
<%= link_to "Stop", stop_time_task_path %>
Finally, in the controller methods, do something like Task.update(start_time: Time.now) (depending on your database structure. And you're done.
Another approach could be creating two forms for Task, each of which represents one of the actions. You could also track the time with Javascript and send both times in once "Stop" is clicked.
I hope this answers your questions. I've tried putting as little code as possible, so to proceed you should try reading the Rails guides or following a simple video tutorial online where the MVC flow is illustrated.
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.
I've built a Rails app that's pretty simple... you send a request to a certain web page, it automatically generates a report and saves it to a file locally on the server. The report that generates is by default created for the current day. What I would like to do is allow the user to enter the date in the url which they would like to have the report generated for turning my little one trick pony into something a little more useful.
As it stands right now I've built all of the useful report generation code into the index action of the the home controller but can't help but feel that it really belongs in a model instead of the controller, but I'm not quite sure how to make that transition.
Any advice from you seasoned rails vets would be greatly appreciated.
Thanks.
The code in my index action....
Basically the index action get's called and my program figures out what day it is, then uses the date to make a request to another API which then returns the necessary information in the form of an object. I then simply go through the object, gather the info I need and generate a text file with that information. The view for the home controller just prints a message saying the report has been generated and provides the path to it.
I would like to stress that I would like to use a url like mysite.com/report/20110803 to provide my controller with the information about the date the report should be run for.
You are thinking correctly that this code belongs to a model. Ideally, the Report model. A controller should generally just feed the view with instance variables, handle flash messages and so on. The logic should be in the model.
That said, i would do it like this: Create a model method that generates the report you want. Then, execute that method in the controller and get back the report you want based on the timestamp that is passed into the model method.
Then, would have this in the controller:
redirect_to new_report_url, :stamp => the_stamp_value
I think you get the idea. You just need to have a simple route that expects a stamp value and you are good to go :)
this time it's about Ruby On Rails.
I have a form in the view "progress.html.erb" and an action called "progress". When the form is sent, the action "progress" is called again.
This must be like that, because I'm asking the user several questions by an automatic system.
Then, when the questioning is finished and the user is done with one seminar of questions, I want to route out of "progress" to "finishing" (where session data is erased and a "happy wishes"-site is shown).
But this won't work because of that routing error. There must be a way, even just rendering won't work :(
The complete system is the following:
I have a box with different panels.
In these panels are cards with questions.
All card get asked to the user.
When all panels evaluate to empty, the user is done.
Please help me!
Yours,
Joern.
When you want to redirect, and then stop rendering, you need to not reach the end of control of the function. The way I do this (for redirecting when someone is somewhere they are not supposed to be, usually) is:
redirect_to(:finishing) and return
optionally, you can do this conditionally:
redirect_to(:finishing) and return if #survey.completed?
I'm not sure what routing error you're getting. Your question/problem isn't very clear. However, it sounds like you want to redirect_to(:finishing) at the end of #progress when the user is "done".