Ruby 自学记录 9 Using partials to clean up duplication in views

https://guides.rubyonrails.org/layouts_and_rendering.html
Create a new html.erb

And change the code in new.html.erb

<h1>New Article</h1>

<%= render 'page1' %>

<%= link_to 'Back', articles_path %>

Just these three lines code.
Refresh the browzer and the from element in the page still.
Why Rails can do that? Look at this sentence:

<%= render 'page1' %>

The name page1 and the Rails will find the page named '_page1.html.erb'

delete article

        DELETE /articles/:id(.:format)                                                                  articles#destroy

We use the delete method for destroying resources, and this route is mapped to the destroy action inside app/controllers/articles_controller.rb, which doesn't exist yet. The destroy method is generally the last CRUD action in the controller, and like the other public CRUD actions, it must be placed before any private or protected methods. Let's add it:

def destroy
  @article = Article.find(params[:id])
  @article.destroy
 
  redirect_to articles_path
end

The index page:

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th></th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Destroy', article_path(article),
                      method: :delete,
                      data:{confirm:'Are you sure?'}%></td>
    </tr>
  <% end %>
</table>

Be attention the :delete not method:delete


Next:
https://guides.rubyonrails.org/getting_started.html#adding-a-second-model

原文地址:https://www.cnblogs.com/ukzq/p/13372586.html