Continuando...
Controller do Aviso
class AvisosController < ApplicationController
before_action :set_aviso, only: [:show, :edit, :update, :destroy]
# GET /avisos
# GET /avisos.json
def index
@avisos = Aviso.all
end
# GET /avisos/1
# GET /avisos/1.json
def show
end
# GET /avisos/new
def new
@aviso = Aviso.new
end
# GET /avisos/1/edit
def edit
end
# POST /avisos
# POST /avisos.json
def create
@aviso = Aviso.new(aviso_params)
respond_to do |format|
if @aviso.save
format.html { redirect_to @aviso, notice: 'Aviso was successfully created.' }
format.json { render :show, status: :created, location: @aviso }
else
format.html { render :new }
format.json { render json: @aviso.errors, status: :unprocessable_entity }
end
end
end
def suatela
@usuarios = Usuario.all
@aviso = Aviso.new
if (params[:texto].blank? && params[:nome].blank?)
@avisos = Aviso.all
else
if not(params[:texto].blank?)
@avisos = Aviso.where("texto like ?","%#{params[:texto]}%")
else
@avisos = Aviso.all
end
end
end
# PATCH/PUT /avisos/1
# PATCH/PUT /avisos/1.json
def update
respond_to do |format|
if @aviso.update(aviso_params)
format.html { redirect_to @aviso, notice: 'Aviso was successfully updated.' }
format.json { render :show, status: :ok, location: @aviso }
else
format.html { render :edit }
format.json { render json: @aviso.errors, status: :unprocessable_entity }
end
end
end
# DELETE /avisos/1
# DELETE /avisos/1.json
def destroy
@aviso.destroy
respond_to do |format|
format.html { redirect_to avisos_url, notice: 'Aviso was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_aviso
@aviso = Aviso.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def aviso_params
params.require(:aviso).permit(:usuario_id, :texto)
end
end
Modelo usuario:
class Usuario < ApplicationRecord
has_many :aviso
end
Modelo aviso:
class Aviso < ApplicationRecord
belongs_to :usuario
end
suatela.html.erb (tela - já com o roteamento feito - principal do meu sistema):
<p id="notice"><%= notice %></p>
<h2> Faça sua pesquisa! </h2>
<%= form_tag(inicio_path ,:method => :get) do %>
<%= text_field_tag 'texto', nil, size: 80, placeholder: 'Digite a sua pesquisa aqui...' %>
<%= submit_tag "Pesquisar!" %>
 
<%= text_field_tag 'nome', nil, size: 20, placeholder: 'Autor' %>
<%= submit_tag "Pesquisar Autor" %>
<% end %>
<br>
<h1>Nosso Quadro de Avisos do Condomínio</h1>
<table>
<thead>
<tr>
<th>ID do autor</th>
<th>Nome do autor</th>
<th>Texto</th>
<th>Criado em:</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @avisos.each do |aviso| %>
<tr>
<td><%= aviso.usuario_id %></td>
<td><%= Usuario.find_by(id: aviso.usuario_id).nome %></td>
<td><%= aviso.texto %></td>
<td><%= aviso.created_at %></td>
<td><%= link_to 'Show', aviso %></td>
<td><%= link_to 'Edit', edit_aviso_path(aviso) %></td>
<td><%= link_to 'Destroy', aviso, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<h1>Quer criar um aviso novo?</h1>
<%= render 'form', aviso: @aviso %>