I am beginning with Ruby on Rails and have a question about the use of the bootstrap's modal.
So my problem is that i have a table and for each row of this table i make a button to display with the modal some other informations about dependent classes but it displays the right informations only for the first row and the same ones for all other rows. I want to make it dynamic and corresponding to the object it deals with.
<table class="table table-hover table-condensed">
<tr>
<th>Id</th>
<th>Name</th>
<th>Description</th>
<th>Priority</th>
<th>State</th>
<th></th>
<th></th>
</tr>
<% #user_stories.each do |user_story| %>
<tr>
<td><%= user_story.id %></td>
<td><%= user_story.name %></td>
<td><%= user_story.description %></td>
<td><%= user_story.priority %></td>
<td><%= user_story.state %></td>
<td>
<div class="btn-group">
<a class="btn btn-primary">Options</a>
<a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>
<ul class="dropdown-menu">
<li><%= link_to 'Show', user_story %></li>
<li class="divider"></li>
<li><%= link_to 'Edit', edit_user_story_path(user_story) %></li>
<li class="divider"></li>
<li> <%= link_to 'Destroy', user_story, :confirm => 'Are you sure?', :method => :delete %></li>
</ul>
</div>
</td>
<td> Global View</td>
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Golbal view : <%= user_story.name %></h3>
</div>
<div class="modal-body">
<% us = user_story.functionalities %>
<% us.each do |f| %>
<span class="label label-info"><%= f.name %></span>
<% t = f.tasks%>
<br/>
<% t.each do |y| %>
<%= y.name %>
<% u = User.where(:id => y.user_id) %>
<%= u.collect {|p| ": #{p.first_name} #{p.last_name}"} %>
<br/>
<% end %>
<br/>
<% end %>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
</tr>
<% end %>
</table>
Any ideas how to fix it ?
Routes:
Backlog::Application.routes.draw do
resources :functionalities
resources :user_stories
resources :users
resources :tasks
end
You have added the modal to every row.
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
But here for the every row the id of the modal is same. So you are getting the same dialog for every row. You need to make dynamic id for for the modals and wherever you are using id in the modal div.
Global View
<div id="myModal<%= user_story.id%>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel<%= user_story.id%>">Golbal view : <%= user_story.name %></h3>
</div>
<div class="modal-body">
<% us = user_story.functionalities %>
<% us.each do |f| %>
<span class="label label-info"><%= f.name %></span>
<% t = f.tasks%>
<br/>
<% t.each do |y| %>
<%= y.name %>
<% u = User.where(:id => y.user_id) %>
<%= u.collect {|p| ": #{p.first_name} #{p.last_name}"} %>
<br/>
<% end %>
<br/>
<% end %>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
Related
I was able to create reviews to book in the proper way. But I'm unable to create and edit reviews in modals.
I was able load the new review form in the modal but it only loads if a book already has reviews. If the books has no reviews then modal button won't work.
I get this error which I don't fully understand
First argument in form cannot contain nil or be empty
pointing to the edit form line
<%= form_for([#book, #review], remote: true) do |f| %>
I've tried to implement the modals for the book model which work well but the reviews doesn't.
books_controller.rb
def show
#reviews = #book.reviews
end
books/show.html.erb
<section id="reviews-section">
<div class="review-form">
<% if user_signed_in? %>
<!-- Button trigger modal -->
<button type="button" class="btn btn-secondary btn-sm" data-toggle="modal" data-target="#mynewreview">
Write your review
</button>
<% else %>
<p>You need to <%= link_to 'sign in', new_user_session_path, class: "btn btn-warning btn-sm" %> before writing a review buddy :)</p>
<br>
<% end %>
</div>
<br>
<div id="reviews_panel">
<%= render #reviews %>
</div>
</section>
reviews/_review.html.erb
<% if user_signed_in? %>
<% if review.user_id == current_user.id %>
<div class="btn-group mr-2" role="group">
<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#editreview_<%= review.id %>">Edit Modal</button>
<%= link_to 'Delete Review', book_review_path(review.book, review), class: "btn btn-sm btn-danger", method: :delete, remote: true, data: { confirm: "Are you sure?" } %>
</div>
<% end %>
<% end %>
<%= render 'reviews/widgets/new_review_modal' %>
<%= render 'reviews/widgets/edit_review_modal' %>
reviews/widgets/_new_review_modal
<%= form_for([#book, #book.reviews.build], remote: true) do |f| %>
<!-- Modal -->
<div class="modal fade" id="mynewreview" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Review</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div id="rating-form">
<label>Rating:</label>
</div>
<div class="field">
<p><%= f.text_area :comment, placeholder: 'Write comment...', class: "form-control review-text-field" %></p>
</div>
</div>
<div class="modal-footer">
<div class="actions">
<%= f.button "Submit Review", class: "btn btn-success btn-sm", data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Submitting Review..."} %>
</div>
</div>
</div>
</div>
</div>
<% end %>
<script>
$('#rating-form').raty({
path: '/assets/',
scoreName: 'review[rating]'
});
</script>
reviews/widgets/_edit_review_modal
<%= form_for([#book, #review], remote: true) do |f| %>
<!-- Modal -->
<div class="modal fade" id="editreview_<%= review.id %>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit your review</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div id="rating-form">
<label>Rating:</label>
</div>
<div class="field">
<p><%= f.text_area :comment, placeholder: 'Write comment...', class: "form-control review-text-field" %></p>
</div>
</div>
<div class="modal-footer">
<div class="actions">
<%= f.button "Submit Review", class: "btn btn-success btn-sm", data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Submitting Review..."} %>
</div>
</div>
</div>
</div>
</div>
<% end %>
<script>
$('#rating-form').raty({
path: '/assets/',
scoreName: 'review[rating]',
score: <%= #review.rating %>
});
</script>
in reviews_controller > edit action you must find #book and #review as well.
def edit
#review = #book.reviews.find(params[:id])
end
It looks like you are showing all reviews in show page of book. and you want to do edit with ajax. In that case you must be using some kind of loop and you must be assigning a single review object to any variable. you need to use that variable in form object
<%= #reviewes.each do |review| %>
<% form_for [#book, review] ... do %>
...
<% end %>
<% end %>
Collapse is only working on the first button, and I know it's because only the first div id is being set for all buttons. How do I iterate and make new div id's for each element when using hash.each?
Here's my erb:
<% #contacts.each do |category, hash| %>
<div class="panel-group" id=accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><%= category %></h3>
</div>
<div class="panel-body">
<ul class="list-group">
<% hash.each do |contact| %>
<li class="list-group-item" style="border: none">
<button type="button" class="btn btn-info" data-toggle="collapse" data-parent="#accordion" data-target="#demo"><%= contact['name'] %></button>
<div id="demo" class="panel-collapse collapse">
<div class="panel-body"><%= contact['email'] %></br><%= contact['ext'] %>
</div>
</div>
</li>
<% end %>
</ul>
</div>
</div>
</div>
<% end %>
Here's the corresponding code from my .rb:
get '/contact' do
contact = Contacts.new
#contacts = contact.getContacts
#contacts.each { |s| puts "get /contact found contact #{s.last.first['name']}" }
erb :contact
try
<% hash.each_with_index do |contact, index| %>
<li class="list-group-item" style="border: none">
<button type="button" class="btn btn-info" data-toggle="collapse" data-parent="#accordion" data-target="#collapse<%= index %>"><%= contact['name'] %></button>
<div id="collapse<%= index %>" class="panel-collapse collapse">
<div class="panel-body"><%= contact['email'] %></br><%= contact['ext'] %>
</div>
</div>
</li>
<% end %>
In Rails 4 I'm trying to render show.html.erb on my index page inside a bootstrap modal.
index.html.erb:
<% #links.each do |item| %>
<h4><%= link_to (item.title), '#myModal', 'data-toggle' => 'modal' %>
Submitted <%= time_ago_in_words(item.created_at) %> ago
</h4>
<div class="modal fade" id="myModal" tabindex="-1" role="document" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<%= render :partial => 'show_modal', :locals => { :item => item } %>
</div>
</div>
</div>
</div>
</div>
<% end %>
_show_modal.html.erb:
<div class="col-md-offset-2 col-md-4" id="telegraph">
<h1><%= item.title %></h1>
<p><%= item.content %></p>
</div>
Currently, the modal is showing nicely, but no matter which link I click, the modal only ever displays the data for the very first link inside #links
Any help would be great.
You need to name each modal differently.
Try setting each modals id to myModal-id where id is the id of the item
For example:
<h4><%= link_to (item.title), "#myModal-#{item.id}", 'data-toggle' => 'modal' %>
Submitted <%= time_ago_in_words(item.created_at) %> ago
</h4>
and
<div class="modal fade" id="myModal-<%= item.id %>" tabindex="-1" role="document" aria-labelledby="myModalLabel">
I have the code below and I want to create an tag between the div with the class 'box'.
I cant just use the link_to method because I want this link to encompass all the html code from the to its respective ending . So anyone who clicks within that box gets transported to the equivalent of: <%= link_to( 'go', { action: :edit, id: course.id }) %>. I tried finding this else where but failed.
<% #courses.in_groups_of(3, false) do |group| %> <!--Takes groups of three courses -->
<div class="row">
<% group.each do |course| %>
<div class="col-xs-12 col-sm-6 col-lg-4">
<div class="box">
<div class="icon">
<div class="image"><span class="glyphicon glyphicon-list-alt btn-lg white"></span></div>
<div class="info">
<h3 class="title"><%= course.name %> | GPA: <%= course.gpa.to_f %></h3>
<p>
<%= course.description %>
</p>
<div class="more">
<a href="#" title="Title Link"><i class="fa fa-plus"></i> Details
</a>
<%= link_to( 'go', { action: :edit, id: course.id }) %>
</div>
</div>
</div>
<div class="space"></div>
</div>
</div>
<% end %>
</div>
<% end %>
<%= link_to({ action: :edit, id: course.id }) do %>
<div class="box">
<div class="icon">
<div class="image"><span class="glyphicon glyphicon-list-alt btn-lg white"></span></div>
<div class="info">
<h3 class="title"><%= course.name %> | GPA: <%= course.gpa.to_f %></h3>
<p>
<%= course.description %>
</p>
<div class="more">
<i class="fa fa-plus"></i> Details
</div>
</div>
</div>
<% end %>
I am not sure I understand your question, but I think you are asking to enclose your html code into a link_to tag. So try this:
<%= link_to path_to_land_on do %>
<div class='box'>
// other code
</div>
<% end %>
Replace path_to_land_on with { action: :edit, id: 2 } or path
I have managed to apply gmaps4rails in my application
FIRST METHOD
I have a map action like this:
def map
#family = Family.all.to_gmaps4rails do |family,marker|
marker.infowindow render_to_string(:partial => "/layouts/info", :locals => { :family => family})
end
end
in my _info.html.erb:
<h5><%=family.location%></h5>
<%=link_to "View Details","#myModal",class: "btn btn-success",data: {toggle: "modal"} %>
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel" style="color:#9D5641;margin-top:10px;">Family Details</h3>
<h5>Family ID : <%=family.famid%></h5>
</div>
<div class="modal-body">
<%= family.persons.count %>
<table class="table table-hover">
<tr>
<th>Name</th>
<th>Mobile No.</th>
<th>Photo</th>
</tr>
<% family.persons.each do |person| %>
<tr>
<td><%=person.name%></td>
<td><%=person.mobnum%></td>
<td><%=person.photo%></td>
</tr>
<%end%>
</table>
</div>
<div class="modal-footer">
<h5><%=link_to "Veiw Full Details", {controller: :managedb ,action: :details, famid: family.famid} %></h5>
</div>
</div>
The bootstrap's modal is rendered fine but with a glitch. It looks like this:
The modal is supposed to be on top of the grey color layer.
I guess this is happening beacause I'm rendering the modal in the infowindow partial. That is why it's css properties are that of the infowindow. How do I make it work as normal modal.
I can remove the background-color which we get once the modal is activated by adding:
.modal-backdrop {background: none;}
to my css file.
But the **modal is not clickable. I cannot click on the links in it and so on.How to fix this?
ALTERNATE METHOD I TRIED
map action
def map
#family = Family.all.to_gmaps4rails do |family,marker|
#fam=family
marker.infowindow render_to_string(:partial => "/layouts/map", :locals => { :family => family})
end
end
_info.html.erb
<h5><%=family.location%></h5>
<%=link_to "View Details","#myModal",class: "btn btn-success",data: {toggle: "modal"} %>
map.html.erb
<div class="container">
<div class="row">
<%= gmaps4rails(#family) %>
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel" style="color:#9D5641;margin-top:10px;">Family Details</h3>
<h5>Family ID : <%=#fam.famid%></h5>
</div>
<div class="modal-body">
<table class="table table-hover">
<tr>
<th>Name</th>
<th>Mobile No.</th>
<th>Photo</th>
</tr>
<% #fam.persons.each do |person| %>
<tr>
<td><%=person.name%></td>
<td><%=person.mobnum%></td>
<td><%=person.photo%></td>
</tr>
<%end%>
</table>
</div>
<div class="modal-footer">
<h5><%=link_to "Veiw Full Details", {controller: :managedb ,action: :details, famid: #fam.famid} %></h5>
</div>
</div>
</div>
Now the modal renders properly but it only has the last family in #fam variable.
How can I pass the family id as parameter from the modal's link?
Finally I did it with the first method.
Altered few css properties of modal
.modal-backdrop {background: none;z-index:-1;}
and
#myModal {z-index:1;}
Now all the elements in the modal are clickable like a normal bootstrap-modal.