Editing
Ruby class 2011 02 22
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Let's begin == Run the following: <pre>$ rails new rudo $ cd rudo $ rails generate model task</pre> Then add the following lines to create_tasks migration: <pre>create_table :tasks do |t| t.string :title t.string :description t.integer :time_required t.integer :value t.datetime :due_date t.datetime :done_date t.boolean :priority_item t.references :project # ^^ this is the same as t.integer :project_id t.timestamps end</pre> Alright, now let's create the other migrations: <pre>$ rails generate model project name:string description:string $ rails generate model tag name:string description:string $ rails generate model tagging</pre> Now let's take a look at the migrations... See the fields automatically added to the migrations We'll need to modify the create_taggings migration, though: <pre>create_table :taggings, :id => false do |t| # can also be t.integer :tag_id, :task_id t.references :tag t.references :task t.timestamps end</pre> Okay, let's try to migrate: <pre>$ rake db:migrate</pre> Alright, now let's get our models associated: ==== app/models/task.rb ==== <pre>has_many :taggings has_many :tags, :through => :taggings belongs_to :project</pre> ==== app/models/tagging.rb ==== <pre>belongs_to :tags belongs_to :task</pre> ==== app/models/project.rb ==== <pre>has_many :tasks</pre> And now our controller: <pre>$ rails generate controller tasks</pre> Edit the controller and add the methods we're going to want: ==== app/controllers/tasks_controller.rb ==== <pre>def show @tasks = Task.all end def create @task = Task.new(params[:task]) end def done Task.find(params[:id]).mark_done end</pre> And now for our primary view: ==== app/views/task/show.html.erb ==== <pre><h1> Tasks: </h1> <% @tasks.each do |task| %> <h3><%= task.title %></h3> Description: <%= task.description %></br> Due: <%= task.due_date %></br> Priority Item: <%= task.priority_item %></br> Project: <%= task.project.name %></br> Tags: <%= task.tags %></br> <% end %></pre> Okay, so now what? Ah, yes, routes. First: <pre>$ rm public/index.html</pre> Now, inside our routes file: ==== config/routes.rb ==== <pre>root :to => "tasks#show"</pre> Alright, so now we have our view, but not much to see. Let's add some tasks from the console. But why not make it possible to do it from our page, seeing as how that's how it will get used. Let's look back at our scratch project.
Summary:
Please note that all contributions to Noisebridge are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
Noisebridge:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Request account
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Dig in!
Noisebridge
- Status: MOVED
- Donate
- ABOUT
- Accessibility
- Vision
- Blog
Manual
MANUAL
Visitors
Participation
Community Standards
Channels
Operations
Events
EVENTS
Guilds
GUILDS
- Meta
- Electronics
- Fabrication
- Games
- Music
- Library
- Neuro
- Philosophy
- Funding
- Art
- Crypto
- Documentation/Wiki
Wiki
Recent Changes
Random Page
Help
Categories
(Edit)
Tools
What links here
Related changes
Special pages
Page information