В настоящее время у меня есть БД в Ruby on Rails, однако у меня возникли проблемы с документацией о том, как сделать многое, кроме списка всех элементов в БД. Я все еще новичок в этом языке в целом и хочу, чтобы мне не нужно было так много помогать, но вот оно. Мой уместна код выглядит следующим образом:Поиск в Ruby-on-Rails DB
migrate/(DB name)
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.string :title
t.text :text
t.timestamps null: false
end
end
end
articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
Article.search(params[:id])
end
def show
@article = Article.find(params[:search])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(params.require(:article).permit(:title, :text))
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
article.rb
class Article < ActiveRecord::Base
validates :title, presence: true,
length: { minimum: 5 }
def self.search(search)
if search
@article = Article.where('name LIKE ?', "%#{search}%")
else
@article = Article.all
end
end
end
index.html.rb
<h1>Listing articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></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 %>
<%= form_tag articles_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</table>
Спасибо за любую помощь заранее!
ha, вы пропустили «имя»;) –
Правда, просто скопировано, что из вопроса OP –
теперь я получаю 'Параметры: {" search "=>" asd "," utf8 "=>" ✓ " } Задание статьи (0,5 мс) ВЫБРАТЬ «статьи». * FROM «articles» 'на бэкэнд без изменений на лицевой стороне –