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.
Related
I am working on Todo app now and I have troubles. After sign in, I am on persons profile(first controller), on it I have button for new project(projects controller-2d controller) and after pressing it, appears button for new tasks(task controller-3d controller). How I can put all of this 3 controller's views on one page. Here an example of what I mean(approximately):http://todo.kzotov.ru/
You can put anything you want in the view. You could eager load the projects and tasks and put it all on the profile page. You also don't have to map controllers and views to models, so if the PersonsController or whatever is not what you're looking for, maybe do something more specific like ProfilesController and host all this functionality there.
MVC
You'll be best reading up on the MVC programming pattern -
The bottom line is that if you send a request to your application, it will only hit one controller#action. Your multiple "controllers" should not be something to consider - you should only look at the single controller action you're accessing at that specific time.
To be more specific about this, let me detail how it all works...
OOP
Ruby (on top of which Rails is a framework), is object orientated.
This is not just a fancy phrase - it's a real pattern of programming, which allows you to focus the flow of your application around the data / objects you want to create. The objects in Rails are derived from your Models - collating & organizing the respective data for your controllers
In order to understand how Rails works - you need to appreciate that everything you do is based on objects. Your routes, actions & data all work together to provide the end-user experience we know from Rails. How that happens is down to you.
Specifically, you want to look what what you're accessing
You don't want to load multiple controllers - you want to build several models and show those. This gives you the ability to show the HTML elements / files you want:
Recommendation
I would make sure you can put all your activity on your single view, which will then mean you have to determine your controller's data in order to provide you with the data you need to show:
#app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
def index
#your index
end
end
#app/views/profile/index.html.erb
<%= link_to "task", task_path %>
What you'll probably want to do is create a separate route / method to give them the ability to pull back ajax data when the initial button was clicked. I can detail this if you need it, but what I've given you should be ample food for thought
My knowledge of Rails is pretty basic, but I have to fix a problem in a Rails project and the programmer can not be reached. So I'm trying to fix it myself, but I'm stuck.
The project revolves around user being able to add pictures to competitions, and to be able to vote on those pictures. The plan was to have to voting on the same page as the images, but this gives a few bugs in the JS and slow performance. So I want to have the voting and the overview on two different pages.
The problem is that I can't figure out how to create another page inside the views > competitions folder and link it up with the rest of the project. The easiest solution for me would be to copy the show.html.haml and paste it like votepage.html.haml but obviously that isn't so easy.
in the view > competitions folder there's an index.html.haml file, this displays a list of current competitions and gives a admin the ability to remove, add or edit certain competitions. When a user clicks on a link to a competition this gets rendered in the show.html.haml. On this page all the images that have been uploaded in that competition are shown. On that page I want a link that refers to the voting section. When a user clicks that link it should go to the votepage.html.haml (which is 100% the same as the show.html.haml but with different styling and added javascript). For now there's no need to actually make the voting work, "faking" it through front-end is good enough.
TLDR: I want to copy/paste a page in a view, but I don't know how to hook it up to the project.
Update1. I've used the console command rails generate controller competitions votepage which created a votepage for me. I could visit this one as well on http://localhost:3000/competitions/votepage
With the following code
- #competitions.each do |competition|
#container.js-masonry
.painting.item
= link_to competition do
- competition.pictures.shuffle.each do |picture|
= image_tag(picture.image_url)
I can insert images from the competitions in the page. But the problem is that I gets images from all competitions. And not so much competitions/1 , competitions/2 etc.
What you're missing is updating the routes file so that Rails knows what you want
Views:
competitions/
show.html.haml
vote.html.haml
...
Routes:
resources :competitions do
get :vote, on: :member
end
Member makes it behave like the show action, requiring a format like competitions/:id/vote
Edit:
You want to do the routes like above, but in the controller, make sure you get the competition from the id that will get passed
Controller:
def vote
#competition = Competition.find(params[:id])
end
And then instead of looping through all the competitions, you can just take the loop out and reference #competition
The basic answer is that you also need to copy the show method from app/controllers/competitions and make a votepage method with the contents in the same file.
This guide will help explain how views get wired (by the controller) to models: http://guides.rubyonrails.org/action_controller_overview.html
I've been stuck on this for about 2 weeks. I need help in the proper way to do this. I have two models. Model one is Leads. Model two is Referrals.
Leads belongs_to referrals
Referrals has_many Leads
In the Lead entry screen there is a partial that displays possible Referrals for the lead to select from. There is an Add Referral link in the partial that brings up a modal using twitter bootstrap as the css framework. From that I am rendering the New action of the Referral controller.
This all works up to this point. What I want to have happen is that I can then enter a new referral, it is saved, the modal closes, and the list of referrals in the partial of the Leads edit/new view is then updated to reflect the new referral that has been added. But at this point I now have nested form_for's. As well as a Referral form that is working off a different controller.
I am not sure how I should approach this. I have been searching and trying various methods for a couple of weeks now. Do I repeat myself and completely rebuild the Referral view and controller under the Leads controller and view? Repetitive code like that is why I switched to rails, and why I feel I'm not looking at this correctly. There has to be a simple way around this. I have looked at using Gems like Draper and Cell, and I've read up on using presenters. But those seem to all be solutions for a dashboard type of view, not what I am trying to accomplish.
Any help or direction would be much appreciated.
Thank you...
This is the way i program this kind of problem.
It works for me, maybe you can adapt to your own situation.
Well, you create action on yout Referral Controller should respond with javascript.
if #referral.save
format.js
This way, you will have a create.js.erb file whitin the views folder where you keep your referral's views.
In the create.js.erb you may have something similar to this:
$('#modal_id').hide(); // Hide the modal or if you prefer, remove from DOM.
$('#referral_list_id').chidren().delete(); // Remove the list of referrals.
$('#referral_list_id').html(
"<%= escape_javascript(render('referrals_list')) %>"
); // Render a partial with the new content from your controller.
Your form for a new referral inside your modal, should be remote too:
<%= form_for #referral, :remote => true do %>
Maybe you will run into some gotchas while implementing this way, but i believe it is kinda easy to solve.
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
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".