• Ruby 自学记录 7


    最近面了家公司,用Ruby on Rails,感觉环境很好,HR流程也棒,还有大牛...
    而且他们招聘的是Java,python,并且说明用Ruby做东西.
    这几天看了下,ROR(Ruby on Rails)真的很不错!
    I wishes they give me the offer ~


    rails routes

     article GET    /articles/:id(.:format)                                                                  articles#show
    

    article_controller

    class ArticlesController < ApplicationController
      # define a action
      def new
    
      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
        @article.save
        redirect_to @article
      end
    
      private
      def article_params
        params.require(:article).permit(:title,:text)
      end
    end
    
    

    <h1>Listing Articles</h1>
    
    <h1>Hello, Rails!</h1>
    <%= link_to 'My Blog', controller: 'articles' %>
    
    <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 am hungry,the ROR record as this now.

    class ArticlesController < ApplicationController
      # define a action
      def 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
        @article.save
        redirect_to @article
      end
    
      private
      def article_params
        params.require(:article).permit(:title,:text)
      end
    end
    
    
  • 相关阅读:
    EntityFramework 启用迁移 EnableMigrations 报异常 "No context type was found in the assembly"
    JAVA 访问FTP服务器示例(2)
    NuGet Package Manager 更新错误解决办法
    JAVA 访问FTP服务器示例(1)
    RemoteAttribute 的使用问题
    诡异的 javascript 变量
    javascript apply用法
    Babun 中文乱码
    GSM呼叫过程
    转站博客园
  • 原文地址:https://www.cnblogs.com/ukzq/p/13365362.html
Copyright © 2020-2023  润新知