zoukankan      html  css  js  c++  java
  • Ruby 自学记录 8


    In the other 7 lessons,I know the Ruby on Rails how to work using MVC pattern.
    https://guides.rubyonrails.org/getting_started.html
    In java,the generate model already have same tools to make it, like [MyBatis Generator].It will generate three layers in the project too.
    And reading the Ruby stuff book,the exception will catch using :

    begin
          //expression code
    rescue
          puts "somethings wrong about that code"
    end
    

    Now,I wll update the "Article demo" with document.

    https://guides.rubyonrails.org/getting_started.html#adding-some-validation


    articles_controller.rb

    class ArticlesController < ApplicationController
      # define a action the new page to create a new article [form]
      def new
        @article = Article.new
      end
    
      # index page will show all the articles
      def index
        @articles = Article.all
      end
    
        # after the article submit redirect to this show page
      def show
        @article = Article.find(params[:id])
      end
    
      # action create
      def create
        # render show the parameters
        # render plain: params[:article].inspect
    
        # we need to change the create action to use
        #
        ## params[:article] contains the attributes we're interested in
        @article = Article.new(article_params)
        ## saving the model in the database .save will return boolean
        # if the save saved redirectTo article page
        if @article.save
          redirect_to @article
        else
          # else render page new
          render 'new'
        end
      end
    
      private
      def article_params
        params.require(:article).permit(:title,:text)
      end
    end
    
    

    index.html.erb

    <h1>Listing Articles</h1>
    
    <h1>Hello, Rails!</h1>
    <%= link_to 'My Blog', "https://www.cnblogs.com/ukzq" ,:target=>"_blank" %>
    <br>
    <%= link_to 'Create New Article', new_article_url %>
    
    <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>
        </tr>
      <% end %>
    </table>
    

    I'll come again when I'm full
    to be continued

    This time we point the form to the update action,which is not defined yet but will be very soon.


    Finally, we want to show a link to the edit action in the list of all the articles,
    so let's add that now to index.html.erb to make it appear next to the "Show" link:

    <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>
        </tr>
      <% end %>
    </table>
    


    define a action in controller too:

      def edit
        @article = Article.find(params[:id])
      end
    


    Now, looks like that ↑.

  • 相关阅读:
    超星电子书下载攻略
    快速生成PDF书签
    2016抢票软件分享
    Win10安装framework3.5
    .NET破解之分享给新注册的朋友
    封装JedisClient.提供API实现对redis的操作
    java操作redis集群配置[可配置密码]和工具类
    redis 集群java.lang.NoSuchMethodError:SpringJAR包版本冲突错误解决方法
    linux端口开放指定端口的两种方法
    redis requires ruby version 2.2.2的解决方案
  • 原文地址:https://www.cnblogs.com/ukzq/p/13371630.html
Copyright © 2011-2022 走看看