Rails startup

2805 ワード

==how to start==
rails new  timeline -d postgres
#1.         "config/routes.rb" 
	resources :articles

#2.       
$ bin/rails g controller articles

#3.        new    "app/controllers/articles_controller.rb "
	  def new
	  end
#4.  new       new  "app/views/articles/new.html.erb"
	<h1>New Article</h1>
#5.  new               "app/views/articles/new.html.erb ",        
<%= form_for :article,url: articles_path do |f| %>	#       form_for
          									#   form_for    ,        "article"
    		#   action       form_for     :url   ,articles_path   rake routes   ,    POST  
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>	
  </p>
 
  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>
 
  <p>
    <%= f.submit %>
  </p>
<% end %>
#6.          create  ,       ,   new POST         ,  ,     create  
,“app/controllers/articles_controller.rb“
	def create
	end
#7.       ,        , create   
def create
	render plain: params[:article].inspect
end
create           ”render plain: params[:article].inspect“  。
render           Hash    ,   Hash     plain,      params[:article].inspect。
params              ,   ActiveSupport::HashWithIndifferentAccess   ,          Symbol        。
#8.                ,         ?  articles   。
$ bin/rails generate model Article title:string text:text
           ,             。
$ bin/rake db:migrate
#9.         ,” app/controllers/articles_controller.rb “
def create
  @article = Article.new(params[:article])			#params[:article]       
  @article.save					#@article.save               ,       show   ,     
  redirect_to @article
end
#10.create     ,       Rails             
def create
  @article = Article.new(article_params)
 
  @article.save
  redirect_to @article
end
private
  def article_params
    params.require(:article).permit(:title, :text)
  end
#11.    。     show  
def show
  @article = Article.find(params[:id])	# Article.find           ,      params[:id]         :id   
end
#10. show    ,”app/views/articles/show.html.erb“
<p>
  <strong>Title:</strong>
  <%= @article.title %>
</p>
<p>
  <strong>Text:</strong>
  <%= @article.text %>
</p>
#11.        ,      index  
def index
  @articles = Article.all
end
#12.    ,  index   
<h1>Listing articles</h1>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
  </tr> 
  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
    </tr>
  <% end %>
</table>

#12.