Here, +method+ and +class+ are appended to the query string of the generated URL because you even though you mean to write two hashes, you really only specified one. So you need to tell Ruby which is which by delimiting the first hash (or both) with curly brackets. This will generate the HTML you expect:
Here, +method+ and +class+ are appended to the query string of the generated URL because even though you mean to write two hashes, you really only specified one. So you need to tell Ruby which is which by delimiting the first hash (or both) with curly brackets. This will generate the HTML you expect:
By following along with this guide, you'll create a Rails project called <tt>blog</tt>, a
(very) simple weblog. Before you can start building the application, you need to
...
...
@@ -97,7 +98,7 @@ To verify that you have everything installed correctly, you should be able to ru
$ rails --version
</shell>
If it says something like "Rails 3.2.2" you are ready to continue.
If it says something like "Rails 3.2.3" you are ready to continue.
h4. Creating the Blog Application
...
...
@@ -111,7 +112,7 @@ $ rails new blog
This will create a Rails application called Blog in a directory called blog.
TIP: You can see all of the switches that the Rails application builder accepts by running <tt>rails new -h</tt>.
TIP: You can see all of the command line options that the Rails application builder accepts by running <tt>rails new -h</tt>.
After you create the blog application, switch to its folder to continue work directly in that application:
...
...
@@ -225,7 +226,6 @@ h4. Laying down the ground work
The first thing that you are going to need to create a new post within the application is a place to do that. A great place for that would be at +/posts/new+. If you attempt to navigate to that now -- by visiting "http://localhost:3000/posts/new":http://localhost:3000/posts/new -- Rails will give you a routing error:
!images/getting_started/routing_error_no_route_matches.png(A routing error, no route matches /posts/new)!
This is because there is nowhere inside the routes for the application -- defined inside +config/routes.rb+ -- that defines this route. By default, Rails has no routes configured at all, and so you must define your routes as you need them.
...
...
@@ -240,7 +240,7 @@ This route is a super-simple route: it defines a new route that only responds to
With the route defined, requests can now be made to +/posts/new+ in the application. Navigate to "http://localhost:3000/posts/new":http://localhost:3000/posts/new and you'll see another routing error:
This error is happening because this route need a controller to be defined. The route is attempting to find that controller so it can serve the request, but with the controller undefined, it just can't do that. The solution to this particular problem is simple: you need to create a controller called +PostsController+. You can do this by running this command:
...
...
@@ -259,7 +259,7 @@ A controller is simply a class that is defined to inherit from +ApplicationContr
If you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new now, you'll get a new error:
!images/getting_started/unknown_action_new_for_posts.png(Unknown action new for PostsController!)
!images/getting_started/unknown_action_new_for_posts.png(Unknown action new for PostsController!)!
This error indicates that Rails cannot find the +new+ action inside the +PostsController+ that you just generated. This is because when controllers are generated in Rails they are empty by default, unless you tell it you wanted actions during the generation process.
...
...
@@ -272,7 +272,7 @@ end
With the +new+ method defined in +PostsController+, if you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/new you'll see another error:
!images/getting_started/template_is_missing_posts_new.png(Template is missing for posts/new)
!images/getting_started/template_is_missing_posts_new.png(Template is missing for posts/new)!
You're getting this error now because Rails expects plain actions like this one to have views associated with them to display their information. With no view available, Rails errors out.
...
...
@@ -302,7 +302,9 @@ When you refresh "http://localhost:3000/posts/new":http://localhost:3000/posts/n
h4. The first form
To create a form within this template, you will use a _form builder_. The primary form builder for Rails is provided by a helper method called +form_for+. To use this method, write this code into +app/views/posts/new.html.erb+:
To create a form within this template, you will use a <em>form
builder</em>. The primary form builder for Rails is provided by a helper
method called +form_for+. To use this method, add this code into +app/views/posts/new.html.erb+:
<erb>
<%=form_for:postdo|f|%>
...
...
@@ -346,7 +348,7 @@ By using the +post+ method rather than the +get+ method, Rails will define a rou
With the form and the route for it defined now, you will be able to fill in the form and then click the submit button to begin the process of creating a new post, so go ahead and do that. When you submit the form, you should see a familiar error:
!images/getting_started/unknown_action_create_for_posts(Unknown action create for PostsController)!
!images/getting_started/unknown_action_create_for_posts.png(Unknown action create for PostsController)!
You will now need to create the +create+ action within the +PostsController+ for this to work.
...
...
@@ -385,10 +387,30 @@ If you re-submit the form one more time you'll now no longer get the missing tem
This action is now displaying the parameters for the post that are coming in from the form. However, this isn't really all that helpful. Yes, you can see the parameters but nothing in particular is being done with them.
h4. Creating the Post model
Rails uses models to manage database objects, so if you want to save
data to the database you'll have to create a model. In our blog
application you want to save posts, so you'll create a +Post+ model.
You can create a model with the following command:
<shell>
$ rails generate model Post title:string text:text
</shell>
With that command we told Rails that we want a +Post+ model, which in
turn should have a title attribute of type string, and a text attribute
of type text. Rails in turn responded by creating a bunch of files. For
now, we're only interested in +app/models/post.rb+ and
+db/migrate/20120419084633_create_posts.rb+. The latter is responsible
for creating the dabase structure, which is what we'll look at next.
h4. Running a Migration
One of the products of the +rails generate scaffold+ command is a _database
migration_. Migrations are Ruby classes that are designed to make it simple to
As we've just seen, +rails generate model+ created a _database
migration_ file inside the +db/migrate+ directory.
Migrations are Ruby classes that are designed to make it simple to
create and modify database tables. Rails uses rake commands to run migrations,
and it's possible to undo a migration after it's been applied to your database.
Migration filenames include a timestamp to ensure that they're processed in the
...
...
@@ -401,9 +423,8 @@ yours will have a slightly different name), here's what you'll find:
class CreatePosts <ActiveRecord::Migration
defchange
create_table:postsdo|t|
t.string:name
t.string:title
t.text:content
t.text:text
t.timestamps
end
...
...
@@ -415,7 +436,7 @@ The above migration creates a method named +change+ which will be called when yo
run this migration. The action defined in this method is also reversible, which
means Rails knows how to reverse the change made by this migration, in case you
want to reverse it later. When you run this migration it will create a
+posts+ table with two string columns and a text column. It also creates two
+posts+ table with one string column and a text column. It also creates two
timestamp fields to allow Rails to track post creation and update times. More
information about Rails migrations can be found in the "Rails Database
Migrations":migrations.html guide.
...
...
@@ -442,39 +463,203 @@ command will apply to the database defined in the +development+ section of your
environment, for instance in production, you must explicitly pass it when
invoking the command: <tt>rake db:migrate RAILS_ENV=production</tt>.
h4. Adding a Link
h4. Saving data in the controller
Back into +posts_controller+, we need to change the +create+ action
to use the new +Post+ model to save data in the database. Open that file
and change the +create+ action to look like the following:
<ruby>
def create
@post = Post.new(params[:post])
@post.save
redirect_to :action => :show, :id => @post.id
end
</ruby>
Here's what's going on: every Rails model can be initialized with its
respective attributes, which are automatically mapped to its
database columns. In the first line we do just that (remember that
+params[:post]+ contains the attributes we're interested in). Then,
+@post.save+ is responsible for saving the model in the database.
Finally, on the last line we redirect the user to the +show+ action,
wich we have not defined yet.
TIP: As we'll see later, +@post.save+ returns a boolean indicating
wherever the model was saved or not, and you can (and usually do) take
different actions depending on the result of calling +@post.save+.
h4. Showing posts
Before trying to create a new post, let's add the +show+ action, which
will be responsible for showing our posts. Open +config/routes.rb+
and add the following route:
<ruby>
get "posts/:id" => "posts#show"
</ruby>
The special syntax +:id+ tells rails that this route expects an +:id+
parameter, which in our case will be the id of the post. Note that this
time we had to specify the actual mapping, +posts#show+ because
otherwise Rails would not know which action to render.
As we did before, we need to add the +show+ action in the
+posts_controller+ and its respective view.
<ruby>
def show
@post = Post.find(params[:id])
end
</ruby>
A couple of things to note. We use +Post.find+ to find the post we're
interested in. We also use an instance variable (prefixed by +@+) to
hold our reference to the post object. We do this because Rails will pass all instance
variables to the view.
Now, create a new file +app/view/posts/show.html.erb+ with the following