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

posted @ 2020-07-23 11:39  ukyo--夜王  阅读(127)  评论(0编辑  收藏  举报