Stack Overflow Asked by Aashutosh on December 7, 2021
messages_controller.rb FILE:
class MessagesController < ApplicationController
def index
@messages = Message.all
@message = Message.new
end
def create
@message = Message.new(message_params)
@messages = Message.all
respond_to do |format|
if @message.save
format.html { redirect_to @message, notice: 'message was successfully created'}
format.js
format.json { render :show, status: :created, location: @message }
end
end
end
private
def message_params
params.require(:message).permit(:body)
end
end
index.html.erb FILE:
<table>
<tbody>
<%= render partial: 'message', collection: @messages %>
</tbody>
</table>
<%= render 'form', message: @message %>
# i remove the code from CREATE.JS.ERB but it results in no change in server (port:3000)#
create.js.erb FILE:
$('table#bands tbody').append("<%= j render @message %>");
_message.html.erb FILE:
<tr>
<td><%= message.body %></td>
</tr>
_form.html.erb FILE:
<%= form_with(model: message) do |f| %>
<div class="col-sm-8">
<%= f.text_area :body, class: "form-control", placeholder: "Message Body" %>
</div>
<div class="btn-group">
<%= f.submit class: 'btn btn-primary' %>
</div>
<% end %>
routes.rb FILE:
Rails.application.routes.draw do
resources :messages
root 'messages#index'
end
schema.rb FILE:
ActiveRecord::Schema.define(version: 2020_07_23_045320) do
create_table "messages", force: :cascade do |t|
t.string "body"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end
Ruby on rails
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP