Render a Rails view in a Bootstrap modal - ruby-on-rails

I have hit a brick wall. I've been hacking at this problem for so long now, I'm not even sure how I got to where I am. All I can say is, I've tried all of the below without success:
How to add bootstrap modal with link_to so the link content open in modal ?
How to show twitter bootstrap modal via JS request in rails?
rails link_to :remote
http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote
How do I render "new", "edit" and "delete" views within Bootstrap modals on the "index" view rather than linking to separate pages for each?
Here is my code as it stands now. For now, lets ignore "edit" and "delete" and just focus on "new". When I click the "New" button, a modal with the string "<%= j render "items/new" %>" appears (instead of the form that that ruby statement should render). What am I doing wrong?:
items_controller.rb:
class ItemsController < ApplicationController
def index
#items = Item.all
end
def new
respond_to do |format|
format.js {}
end
end
def create
#item = Item.new(item_params)
if #item.save
flash[:notice] = "'#{#item.name}' saved!"
redirect_to items_path
else
flash[:notice] = "Something went wrong :("
render "index"
end
end
def edit
#item = Item.find(params[:id])
respond_to do |format|
format.js {}
end
end
def update
#item = Item.find(item_params[:id])
if #item.update_attributes(item_params)
flash[:notice] = "Successfully updated #{#item.name}."
redirect_to items_path
else
flash[:notice] = "Oops"
# render "edit"
end
end
private
def item_params
params.require(:item).permit(:name, :bid, :uuid)
end
end
items/index.html.erb
<div class="row">
<div class="col-xs-12">
<%= link_to "New", new_item_path, remote: true, class: "btn btn-success pull-right new", data: { toggle: "modal", target: "#newModal" } %>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<table class="table table-hover items">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>UUID</th>
<th colspan="2">Links</th>
</tr>
</thead>
<tbody>
<% #items.each do |item| %>
<tr>
<td><%= item.id %></td>
<td><%= item.name %>
<!-- edit/remove icons -->
<span class="edit-remove">
<%= link_to edit_item_path(item.id), remote: true, data: { toggle: "modal", target: "#editModal" } do %>
<span class="glyphicon glyphicon-pencil text-muted"></span>
<% end %>
<a href="#">
<span class="glyphicon glyphicon-remove text-muted"></span>
</a>
</span>
</td>
<td><%= item.uuid %></td>
<td><%= link_to "XXX", "http://xxx" %></td>
<td><%= link_to "XXXX", "http://xxx", target: "_blank" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
<!-- newModal skeleton -->
<div class="modal fade" id="newModal">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
<!-- editModal skeleton -->
<div class="modal fade" id="editModal">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
<!-- deleteModal skeleton -->
<div class="modal fade" id="deleteModal">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
items/new.html.erb
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span>×</span></button>
<h4 class="modal-title">New Item</h4>
</div>
<div class="modal-body">
<%= form_for :item, url: { action: "create" } do |f| %>
<div class="form-group">
<%= f.label :name, "Name" %>
<%= f.text_field :name, { class: "form-control" } %>
</div>
<div class="form-group">
<%= f.label :bid, "BID" %>
<%= f.text_field :bid, { class: "form-control" } %>
</div>
<div class="form-group">
<%= f.label :uuid, "UUID" %>
<%= f.text_field :uuid, { class: "form-control" } %>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= submit_tag "Save", class: "btn btn-primary" %>
<% end %>
</div>
javascripts/items.js
$(document).on("page:change", function() {
$("#newModal .modal-content").html('<%= j render "items/new" %>');
});

In the case of new for instance, you want to render a javascript file. For this, you'll need to create items/new.js.erb.
Also, remove ", data: { toggle: "modal", target: "#newModal" }" from your link, we will do that in the javascript.
# new.js.erb
$("#newModal .modal-content").html('<%= j render "items/form" %>');
$("#newModal").modal(); // Or whatever the Bootstrap function is to render the modal
# items/_form.html.slim
# Here you'll put your form
You cannot use "render" on views directly, you should render partials and not views (this is why I asked you to put your form into a partial).

I threw this together and it puts a big '3' in my document 3 seconds after loading it:
<script>
setTimeout(function() {
$("#holder").html("<%= j render(:file => 'things/test.html.erb') %>");
}, 3000);
</script>
<div id="holder></div>
app/views/things/test.html.erb:
<h1><%= 1 + 2 %></h1>
That should get you going.

Related

Edit an object Modal Ruby on Rails

Yoo, I'm trying to make a modal with which I can edit a Note.
But when I click on the edit button. It opens the modal but it only display the first note's infos even clicking on the others note it always redirects me to edit the first note of the table.
I can't seem to figure it out.
Here's my index view
<table class="table mt-5">
<thead>
<tr>
<th><p>Titulo</p></th>
<th><p>Conteudo</p></th>
<th><p>Data</p></th>
<th><p>Prioridade</p></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% current_user.notes.each do |n| %>
<tr>
<td><%= n.title %></td>
<td><%= n.content %></td>
<td><%= n.date %></td>
<td><%= n.priority %></td>
<td> <%= link_to "Editar", edit_note_path(n), class: "btn btn-outline-warning",data: { toggle: "modal", target: "#EditNote", whatever: "#getbootstrap"} %></td>
<td><%= link_to "Apagar", note_path(n), method: :delete, data: { confirm: 'Tem certeza ?' }, class: "btn btn-outline-danger" %>
</td>
</tr>
<div class="modal fade" id="EditNote" tabindex="-1" role="dialog" aria-labelledby="EditingNote" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="CreatingNote">Editar Anotação</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<%= simple_form_for n do |f| %>
<%= f.input :title, label: "Title", placeholder: "Title" %>
<%= f.input :content, placeholder: "Escreve aqui" %>
<%= f.input :date %>
<%= f.input :priority, :collection => %w[Baixa Média Alta] %>
</div>
<div class="modal-footer">
<%= f.submit "Editar", class: "btn btn-warning btn-block" %>
<% end %>
</div>
</div>
</div>
</div>
<% end %>
</tbody>
</table>
Here's my Controller
class NotesController < ApplicationController
before_action :set_note, only: %w[edit update destroy]
def index
#notes = Note.all
#note = Note.new
end
def new
#note = Note.new
end
def create
#note = Note.new(note_params)
#note.user = current_user
if #note.save
redirect_to notes_path
flash[:alert] = "Anotação salva"
else
flash[:alert] = "Tenta de novo"
render :new
end
end
def edit
end
def update
#note.update(note_params)
redirect_to notes_path
end
def destroy
#note.destroy
redirect_to notes_path
end
private
def set_note
#note = Note.find(params[:id])
end
def note_params
params.require(:note).permit(:title, :content, :date, :priority, :user_id)
end
end
Every modal has the id #EditNote. And every edit link has:
data: { toggle: "modal", target: "#EditNote", whatever: "#getbootstrap"}
So, I suppose the click - regardless of which link you're clicking - is always showing the first modal with the id #EditNote which is always the modal for the first note.
Perhaps try:
data: { toggle: "modal", target: "#editNote#{n.id}", whatever: "#getbootstrap"}
...and:
<div class="modal fade" id="editNote#{n.id}" tabindex="-1" role="dialog" aria-labelledby="EditingNote" aria-hidden="true">
So that every modal has a unique id and every link points at that unique id.
BTW, it seems using a link_to isn't really necessary since you're not doing anything in the controller action, but it's probably not doing any harm either. It's just an unnecessary request-response cycle.

Undefined local variable or method `plane' for #<#<Class:0x4409040>:0x43d7720> (Ruby on Rails)

I'm trying to do an edit object in Ruby on Rails using a modal form, when I try to do the link_to edit_plane_path(plane) in planes.html.erb I get the following error:
undefined local variable or method `plane' for #<#:0xd580e98>
Did you mean? #plane
plane_url
#planes
However, trying any of those doesn't really change the fact that I still get an error on the page and I can't even load it.
planes.html.erb
<table>
<thead>
<tr>
<th>Image</th>
<th>Operating agency</th>
<th>Date</th>
<th>Call sign</th>
<th>Country</th>
<th>Info</th>
<th>Action</th>
</tr>
<thead>
<tbody>
<% #planes.each do |p| %>
<tr class="gradeC">
<td>
<%= image_tag(p.image, size: "150x150")%></td>
<td class="w-25"><%= p.provider%>
</td>
<td><%= p.brand%></td>
<td class="center"><%= p.year%></td>
<td class="center"><%= p.call_sign%></td>
<td><%= p.country%></td>
<td><%= p.info%></td>
<td class="text-right">
<div class="btn-group">
<%= link_to 'Edit', edit_plane_path(plane), remote: true, :class =>'btn white btn btn-xs' %>
<button class="btn-white btn btn-xs">Delete</button>
</div>
</td>
</tr>
</tbody>
<% end %>
</table>
_form.html.erb
<!-- FORM EDIT -->
<div id="edit-plane" class="modal fade" aria-hidden="true">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-body">
<div class="row">
<div class="col-sm-12"><h3 class="m-t-none m-b">Edit plane</h3>
</div>
</div>
<%= form_for #plane, url: planes_path, remote: true do |f| %>
<div class="form-group row">
<div class="col-sm-6">
<label>Brand</label>
<%= f.text_field :brand, class:"form-control"%>
</div>
<div class="col-sm-5">
<label>Model</label>
<%= f.text_field :model, class:"form-control"%>
</div>
</div>
<div class="form-group row">
<div class="col-sm-5">
<label>Year</label>
<%= select_tag :hyear, options_for_select(["2015","2016","2017","2018","2019"], "2019"), class:"select2_demo_1 form-control" %>
<%= f.hidden_field :year %>
</div>
<div class="col-sm-6">
<label>Country</label>
<%= select_tag :hcountry, nil, class:"select2_demo_1 form-control" %>
<%= f.hidden_field :country %>
</div>
<script language="javascript">
populateCountries("hcountry");
</script>
</div>
<div class="form-group row">
<div class="col-sm-6">
<label>Provider</label>
<%= select_tag :hprovider, options_for_select([ "Test", "Test2"], "Test"), class:"select2_demo_1 form-control"%></select>
<%= f.hidden_field :provider %>
</div>
<div class="col-sm-5">
<label>Tactical call sign</label>
<%= f.text_field :call_sign, class:"form-control"%>
</div>
</div>
<div class="form-group row">
<div class="col-sm-6">
<label>Extra info</label>
<%= f.text_area :info %>
</div>
<div class="col-sm-5">
<label>Image</label>
<%= f.file_field :image %>
</div>
</div>
<!-- SUBMIT -->
<button class="btn btn-primary btn-lg float-right ml-2" id="cancelbtn">Cancel</button>
<%= f.submit "Submit", id:"edpla", class: 'btn btn-primary btn-lg float-right'%>
</div>
<% end %>
</div>
</div>
</div>
</div>
</div>
</div>
edit.js.erb
// Add the dialog title
$('#edit-plane h3').html("<i class=' glyphicon glyphicon-pencil'></i> Edit Plane");
// Render the edit form
$('.modal-body').html('<%= j render("form") %>');
// Show the dynamic dialog
$('#edit-plane').modal("show");
// Set focus to the first element
$('#edit-plane').on('shown.bs.modal', function () {
$('.first_input').focus()
})
update.js.erb
$('#edit-plane').modal('toggle');
$('#customer_<%= #plane.id %>').replaceWith('<%= j render (#plane) %>')
planes_controller.rb
class PlanesController < ApplicationController
def planes
#plane = Plane.new
#planes = Plane.all
end
def create
#plane = Plane.new(plane_params)
if #plane.save
flash[:success] = "Plane successfully added"
redirect_to :planes => 'post', :action => 'planes'
else
flash[:error] = "Something went wrong"
render 'planes'
end
end
def edit
#plane = Plane.find(params[:id])
end
def update
respond_to do |format|
if #plane.update(plane_params)
format.json { head :no_content }
format.js
else
format.json { render json: #customer.errors.full_messages, status: :unprocessable_entity }
end
end
end
private
def plane_params
params.require(:plane).permit(:brand, :model, :provider, :call_sign, :user, :country, :image, :info, :year)
end
end
My goal is to edit a plane in the table through a modal form, but I can't even show the page at the moment.
Here:
<% #planes.each do |p| %>
...you are passing each plane as p. Therefore, try:
<%= link_to 'Edit', edit_plane_path(p), remote: true, class: 'btn white btn btn-xs' %>

How to perform POST operation in Rails using AJAX

Scenario
I have created a simple application for learning Rails and Bootstrap. What this application does is it allows users to give nicknames to celebrities and like the nicknames as well. You can check it at http://celebnicknames.herokuapp.com
I have got a link_to button for posting likes for a celebrity in my home/index.html.erb as:
<%= link_to home_updatelike_path(id: nickname.id, search: #search ), method: :post, remote: true, class: "update_like btn btn-xs btn3d pull-right" do %>
<i class="glyphicon glyphicon-thumbs-up"></i>
<% end %>
The corresponding code in my home_controller.rb for the post method is:
def updatelike
#like = Like.new(:nickname_id => params[:id], :ip_address => request.remote_ip)
#like.save
respond_to do |format|
format.html { redirect_to root_path("utf8"=>"✓", "search"=>params[:search], "commit"=>"Search") }
format.json { head :no_content }
format.js { render :layout => false }
end
end
The code in my updatelike.js.erb is as:
$('.update_like').bind('ajax:success', function() {
});
Now, what I want to achieve is to submit a like without performing a page reload.
Problem:
Whenever I press the button, the database operation is done, but the change is not reflected in the page unless I reload it. Please guide me through this noob question and also provide links to resources which explain using AJAX with Rails.
Edit
Here is the entire home/index.html.erb
<div class="jumbotron col-md-6 col-md-offset-3">
<h1 class='text-center'>Nicknamer</h1>
</div>
<div class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2 bottom50">
<%= form_tag(root_path, :method => "get", id: "search-form", type: "search") do %>
<div class = "form-group" >
<div class = "col-sm-9 " >
<%= text_field_tag :search, params[:search], placeholder: "Name", class: "form-control" %>
</div>
<div class = "col-sm-3" >
<%= submit_tag "Search", class: "btn btn-default btn-block btn-success"%>
</div>
</div>
<% end %>
</div>
</div>
<% #names.each do |name| %>
<div class="col-md-6 col-xs-8 col-md-offset-3 col-xs-offset-2 bottom50">
<!-- ===== vCard Navigation ===== -->
<div class="row w">
<div class="col-md-4">
<% if name.sex == true %>
<%= image_tag("mal.jpg", :alt => "", class: "img-responsive") %>
<% else %>
<%= image_tag("fem.jpg", :alt => "", class: "img-responsive") %>
<% end %>
</div><!-- col-md-4 -->
<!-- ===== vCard Content ===== -->
<div class="col-md-8">
<div class="row w1">
<div class="col-xs-10 col-xs-offset-1">
<h3><%= "#{name.name}" %></h3>
<hr>
<h5>Also known as</h5>
<% name.nicknames.each do |nickname| %>
<div class = "row w2 bottom10">
<% if nickname.name_id == name.id %>
<div class="col-xs-7">
<%= nickname.nickname %>
</div>
<div class="col-xs-3 text-right">
<%= pluralize(nickname.likes.count, 'like') %>
</div>
<div class="col-xs-1">
<%= link_to home_updatelike_path(id: nickname.id, search: #search ), method: :post, remote: true, class: "update_like btn btn-xs btn3d pull-right" do %>
<i class="glyphicon glyphicon-thumbs-up"></i>
<% end %>
</div>
<% end %><!-- if -->
</div><!-- row w2 -->
<% end %><!-- do -->
<div class = "row w3 bottom30">
<%= form_for #addnickname, as: :addnickname, url: {action: "addnickname"} do |f| %>
<div class = "form-group" >
<%= f.hidden_field :name_id, :value => name.id %>
<%= f.hidden_field :search, :value => #search %>
<div class = "col-xs-9">
<%= f.text_field :nickname , :required => true, class: "form-control" %>
</div>
<div class = "col-xs-3">
<%= f.submit "Add", class: "btn btn-default btn-info"%>
</div>
</div>
<% end %>
</div>
</div><!-- col-xs-10 -->
</div><!-- row w1 -->
</div><!-- col-md-8 -->
</div><!-- row w -->
</div><!-- col-lg-6 -->
<% end %>
</div><!-- /.container -->
and this is the entire home_controller.rb
class HomeController < ApplicationController
def index
#addnickname = Nickname.new
if params[:search]
#search = params[:search]
#names = Name.search(params[:search])
else
#names = Name.all
end
end
def addnickname
#addnickname = Nickname.new(:nickname => params[:addnickname][:nickname], :name_id => params[:addnickname][:name_id])
if #addnickname.save
redirect_to root_path("utf8"=>"✓", "search"=>params[:addnickname][:search], "commit"=>"Search")
else
render :new
end
end
def updatelike
#like = Like.new(:nickname_id => params[:id], :ip_address => request.remote_ip)
#like.save
respond_to do |format|
format.html { redirect_to root_path("utf8"=>"✓", "search"=>params[:search], "commit"=>"Search") }
format.json { head :no_content }
format.js { render :layout => false }
end
end
private
def addnickname_params
params.require(:addnickname).permit(:name_id, :nickname, :search)
end
end
Ok, so you need a way to identify the div with the number that needs to change:
<div class="col-xs-3 text-right">
<%= pluralize(nickname.likes.count, 'like') %>
</div>
The easiest way to do this would be to have an id attribute on the div that corresponds to the (unique) database id of the nickname. Rails has a helper method for this called div_for. So you could change the above code to this:
<%= div_for(nickname, class: "col-xs-3 text-right") do %>
<%= pluralize(nickname.likes.count, 'like') %>
<% end %>
That will generate a div with an id like nickname_1 (if this id of the nickname is 1).
Now in your javascript template you can easily target the div that needs to be updated:
$("nickname_#{#like.nickname.id}").html(#like.nickname.likes.count);
The ajax:success binding is unnecessary here since you are rendering a javascript template. In the template you can access instance variables set in the controller - just like you can from an HTML template. The above code is meant to replace the content of the div that contains the likes count with the new count.

Rails+Bootstrap Modal renders but doesn't show

I can get the modal to render in the DOM, but I can't get it to show, I get it to flash for an instant, but it doesn't stay. Here is the relevant code.
tasks_controller.rb
def edit
#task = Task.find(params[:id])
respond_to do |format|
format.html
format.js
end
end
edit.js.erb
$(document).ready( function() {
$("#edit_task_modal").html("<%= escape_javascript(render 'tasks/edit_task_modal') %>");
$("#edit_task_modal").modal('show');
});
_edit_task_modal.html.erb
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button id="close_edit_task_modal" type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="edit_task_modal_label">Edit Task</h4>
</div>
<%= form_for(:task, url: {action: 'update', id: #task.id},:html => {:class => 'form-horizontal'}) do |f| %>
<div class="modal-body">
<%= render(partial: 'form', locals: {f: f}) %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= f.submit 'Save Changes', class: 'btn btn-primary' %>
</div>
<% end %>
</div>
</div>
_task_div.html.erb
<div class="task well">
<a>Title: <%= "#{task.title}" %></a><br/>
Description: <%= "#{task.description}" %> <br>
Status: <%= "#{task.status}" %> <br>
Due Date: <%= "#{task.due_date}" %> <br>
Completed Date: <%= "#{task.completed_date}" %> <br>
<div style='text-align:right;'>
<%= link_to 'Edit', {controller: 'tasks', action: 'edit', id: task.id}, {:remote => true, 'data-toggle' => "modal", 'data-target' => '#edit_task_modal'} %>
</div>
</div>
show.html.erb
<div id="edit_task_modal" class="modal fade" role="dialog" aria-labelledby="edit_task_modal_label" aria-hidden="true" tabindex="-1"></div>
EDIT: Added modal code
Turns out all I had to do was remove 'data-toggle'='modal' from the link to the modal. The javascript was showing it, then the html would toggle it making it hidden again.

Trying to create an edit modal on my index page using rails and bootstrap 3

So far I've been able to make a modal that will 'create' a facility from the index page when user clicks the 'add new facility' button. Now my problem is trying to do the same thing except I want the user to be able to edit their facility using a modal from the index page as well. At this point when I click my 'edit' button on the webpage it opens a modal, but it is still the 'create facility' modal and not the 'edit modal'. Here's code:
views\facilities\index.html.erb
<h1>Facilities</h1>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Abbr.</th>
<th>Status</th>
<th>Message</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #facilities.each do |facility| %>
<tr>
<td><%= facility.name %></td>
<td><%= facility.abbr %></td>
<td><%= facility.status %></td>
<td><%= truncate(facility.message, :length => 60, :seperator => ' ') %></td>
<td><%= link_to '<button class="btn btn-primary btn-edit" data-toggle="modal" data-target="#myModal2">Edit</button>'.html_safe, edit_facility_path(facility) %></td>
<td><%= link_to '<button class="btn btn-danger btn-edit">Destroy</button>'.html_safe, facility, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<div class="center" style="margin-top: 30px; margin-bottom: 80px;">
<!-- Future Modal Button -->
<%= link_to '<button class="center btn btn-success btn-fac" data-toggle="modal" data-target="#myModal1">Add New Facility</button>'.html_safe, new_facility_path %>
<!--<%= link_to '<button class="center btn btn-success btn-fac">Add New Facility</button>'.html_safe, new_facility_path %> -->
</div>
<!-- Create Modal -->
<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Add New Facility</h4>
</div>
<div class="modal-body">
<div class="small-container">
<%= simple_form_for(#facility) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :abbr %>
<%= f.input :status, collection: ["Open", "Close", "Custom"], selected: ["Open"] %>
<%= f.input :message %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- Edit Modal -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<div class="small-container">
<%= simple_form_for(#facility) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :abbr %>
<%= f.input :status, collection: ["Open", "Close", "Custom"], selected: ["Open"] %>
<%= f.input :message %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Here's controllers\facilities_controller.rb
class FacilitiesController < ApplicationController
before_action :set_facility, only: [:show, :edit, :update, :destroy]
# GET /facilities
# GET /facilities.json
def index
#facility = Facility.new
#facilities = current_user.facilities.all
end
# GET /facilities/1
# GET /facilities/1.json
def show
#facility = current_user.facilities.find(params[:id]) rescue redirect_to(facilities_path)
end
# GET /facilities/new
def new
#facility = Facility.new
end
# GET /facilities/1/edit
def edit
#facility = current_user.facilities.find(params[:id]) rescue redirect_to(facilities_path)
end
# POST /facilities
# POST /facilities.json
def create
#facility = current_user.facilities.new(facility_params)
respond_to do |format|
if #facility.save
format.html { redirect_to facilities_path, notice: 'Facility was successfully created.' }
format.json { render action: 'show', status: :created, location: #facility }
else
format.html { render action: 'new' }
format.json { render json: #facility.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /facilities/1
# PATCH/PUT /facilities/1.json
def update
respond_to do |format|
if #facility.update(facility_params)
format.html { redirect_to #facility, notice: 'Facility was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #facility.errors, status: :unprocessable_entity }
end
end
end
# DELETE /facilities/1
# DELETE /facilities/1.json
def destroy
#facility.destroy
respond_to do |format|
format.html { redirect_to facilities_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_facility
#facility = Facility.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def facility_params
params.require(:facility).permit(:name, :abbr, :status, :message)
end
end
Let me know if more information is needed, thanks.

Resources