Ruby on rails RubyonRails-如何计算并返回要显示的值

Ruby on rails RubyonRails-如何计算并返回要显示的值,ruby-on-rails,Ruby On Rails,我不熟悉rails。我有基本的记录。我想做的是,当有人单击“生成”时,它将从数据库中的一个大列表中获取一个标题和一个描述,执行50次,然后在新页面上显示这些标题和描述 我不太清楚这是怎么做到的。这是在控制器中处理的逻辑,还是一个rake任务 到目前为止我拥有的内容(我还设置了所有视图): 路线: Rails.application.routes.draw do mount RailsAdmin::Engine => '/admin', as: 'rails_admin' devis

我不熟悉rails。我有基本的记录。我想做的是,当有人单击“生成”时,它将从数据库中的一个大列表中获取一个标题和一个描述,执行50次,然后在新页面上显示这些标题和描述

我不太清楚这是怎么做到的。这是在控制器中处理的逻辑,还是一个rake任务

到目前为止我拥有的内容(我还设置了所有视图):

路线:

Rails.application.routes.draw do
  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
  devise_for :users
  root to: "home#index"

  resources :articles
end
型号:

  create_table "articles", force: :cascade do |t|
    t.text     "title"
    t.text     "body"
    t.text     "keywords"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "name"
  end
控制器:

class ArticlesController < ApplicationController
  before_action :authenticate_user!
  before_action :find_article, only: [:edit, :update, :show, :destroy]

  def index
    @articles = Article.all
  end
  # Creates article
  def new
    @article = Article.new
  end
  # Saves article
  def create
    @article = Article.new(article_params)
    if @article.save(article_params)
      flash[:notice] = "Successfully created #{@article.title}"
      redirect_to article_path(@article)
    else
      flash[:alert] = "Error creating #{@article.title}"
      render :new
    end
  end
  # renders edit page
  def edit

  end
  #updates article with new info
  def update
    if @article.update_attributes(article_params)
      flash[:notice] = "Successfully updated article!"
      redirect_to article_path(@article)
    else
      flash[:alert] = "Error updating article!"
      render :edit
    end
  end

  # renders the article
  def show
  end

  # deletes the article
  def destroy
    @article = Article.find(params[:id])
    if @article.destroy
      flash[:notice] = "Successfully deleted!"
      redirect_to articles_path
    else
      flash[:alert] = "Error deleting article"
    end
  end

  private

  def article_params
    params.require(:article).permit(:title,:body,:keywords,:name)
  end

  def find_article
    @article = Article.find(params[:id])
  end
end
class-ArticlesController
index.html.erb
或其他地方放置一个
生成
按钮

index.html.erb

routes.rb

然后将一个foo方法/操作添加到您的文章控制器

def foo
  @articles = Article.take(50) #or some other constraints
end
如果您想在不同的视图(例如foo.html.erb)中呈现这些内容,请在
app/views/articles
path下创建一个。如果将其命名为非
foo
(例如
bar.html.erb
),则应明确将其包含在
foo
操作中

def foo
  @articles = Article.take(50)
  render :bar 
end

在该视图中,以您想要的方式呈现
@articles

它有很多方面-您至少需要一个ui、一个接受该请求的路径和一个db查询来用有意义的内容满足该请求。请分享你到目前为止所做的。谢谢你。我编辑了这篇文章以包括我到目前为止所做的
def foo
  @articles = Article.take(50) #or some other constraints
end
def foo
  @articles = Article.take(50)
  render :bar 
end