Forests and CSS in Ruby on Rails

I made a blogpost with two entries ( title , content ), like this

 rails generate scaffold Post title:string content:text 

It works, but

  • What if I want to add a new input area later? Ex. author or something like that. Where can I add extra fields to my Post relay?

  • I made a style sheet style.css inside my shared directory. He just sat there. How to link it with my file in app/views/post/new.html.erb for example?

  • Where can I find out all the settings for scaffolding in a specific area? Ex. I want the title input field to not contain special characters (e.g. @#$% ) or to not exceed a certain length.

I use windows and rails 3.2.8.

+4
source share
3 answers

Question 3 in 1 :)

1st answer

As others have said, you need to modify the table to have new fields, and this can be easily done with migration :

 rails generate migration AddAuthorToPost author:string 

But you also need to change your views, because it will not contain a new field. The corresponding views are in app/views/posts (find edit.html.erb and new.html.erb ).

Of course you need to migrate to your database:

 rake db:migrate 

Second answer

You can add your style sheets to your views in app/views/posts , but I recommend that you use layouts for your application, layouts are located in app/views/layouts . You can simply create application.html.erb in your layouts directory and include your CSS there.

 <html> <head> <%= stylesheet_link_tag "custom" %> </head> <body> <%= yield %> </body> </html> 

The yield block will contain the actual output of your views, so you must remove these parts from existing views.

Third answer

If you want to control the accepted characters for a specific field, you can do this in validation in your model.

 validates :title, :format => { :with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed" }, :length => { :maximum => 20 } 
+4
source

If you want to add a new field, you need to migrate and add the field to the _form.html.erb file

 rails generate migration add_author_to_posts author:string 

You must create your styles inside app / assets / stylesheets

Read this: http://guides.rubyonrails.org/getting_started.html

And for the transfer: http://guides.rubyonrails.org/migrations.html

In addition, I recommend you: http://railscasts.com/

Welcome aboard!

0
source

The safest way to add columns to an existing table is to simply create a new migration:

 rails g migration add_public_and_private_to_document public:string private:string 

If you use the naming convention add_[column_names]_to_[model] , Rails will develop the appropriate table and create the required migration.

Read here for more information: http://guides.rubyonrails.org/migrations.html

0
source

All Articles