Render another controller to rails - ruby-on-rails

How to render another controller to rails 2.3.2?
I was tried below code.but not work.
app/controllers/vouchers
app/controllers/voucher_payments
app/views/vouchers/show.html.erb
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#test">test</button>
<div class="modal fade" id="test" tabindex="-1" role="dialog" 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">New Payment</h4>
</div>
<div class="modal-body">
<%= render :template => "/voucher_payments/new" %>
</div>
</div>
</div>
</div>
app/views/voucher_payments/new.html.erb
<h1>Hello!!!!!!!!!!!!!</h1>
can you help me?
thanks.

You should have a folder called voucher_payments in app/views
Now, in that app/views/voucher_payments you should have new.html.erb
app/views/voucher_payments/new.html.erb
So, now, the document says you can render the results of an action to a template in app/views/voucher_payments this way:
<%= render :template => 'voucher_payments/new' %>
<%= render "voucher_payments/new" %>
<%= render :partial => 'voucher_payments/new' %>

I too had the same problem, appending .html fixed it, try this
<%= render :template => '/voucher_payments/new.html' %>
Hope that works for you.

Related

how to make a link to bootstrap modal through image with ruby on rails

I'm trying to make a link to bootstrap modal through clicking image.
I'm basically trying to adapt bootstrap demo code to ruby tags.
<%= link_to 'data-toggle' => 'modal', 'data-target' => '#exampleModalLong' do %>
<%= image_tag("https://i.imgur.com/qtVofCH.jpg", :class => "portfolio-image") %>
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
...
</div>
</div>
<% end %>
But this doesnt work. This makes it go to the top of the page, adding
?data-target=%23exampleModalLong&data-toggle=modal
to the url.
Edit1: Now i target the modal with just '#exampleModalLong':
<%= link_to '#exampleModalLong', 'data-toggle' => 'modal' do %>
<%= image_tag("https://i.imgur.com/qtVofCH.jpg", :class => "portfolio-image") %>
<% end %>
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
...
</div>
But it still won't work properly. Atleast it doesn't bring me back on the top of the page. And adds
/#exampleModalLong
to the url.
I don't know why wont it trigger the modal div, when I target it through id.
This solution worked for me Modal bootstrap, image instead of button. Have you tried it?
Be careful to change the id of the modal to have the same in the data-target attribute of your <a> tag and in the id of the <div> for your Modal window.
<!-- Button -->
<a data-toggle="modal" data-target="#exampleModalLong">
<%= image_tag("https://i.imgur.com/qtVofCH.jpg") %>
</a>
<!-- Modal -->
<div class="modal fade" id="exampleModalLong" 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">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

Rails form above Twitter Bootstrap modal

I'm trying to create a newsletter subscription from scratch using rails 4 and Twitter Bootstrap 3 modal.
I created a Subscriber model and subscribers controller which contains a 'create' method.
The button which openes the modal has to appear anytime, so I included it in the navbar that is placed in the application.html.erb layout view file.
This is the code I used for the modal: (in views/layout/application.html.erb)
<div class="modal fade" id="myModal" 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"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Newsletter Subscriptions</h4>
</div>
<% form_tag(controller: 'subscribers', action: 'create') do %>
<div class="modal-body">
<p><%= text_field_tag :email, params[:email] placeholder: "Enter your email address" %></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<p><%= submit_tag "Subscribe", class: "btn btn-primary" %></p>
</div>
<% end %>
</div>
</div>
</div>
For some reason it won't show anything in the modal except the title.
Where am I going wrong?
You just need to use <%= form_tag instead of <% form_tag.
In previous versions of Rails, <% form_tag was used I think, but since form_tag is outputting html, it should be used with <%= %>.

Use Kaminari Pagination within a Bootstrap Modal

I am having some difficulties getting the Kaminari gem to work with my Bootstrap modal. I know that it needs to work primarily with Ajax, but I'm not quite sure what to do on this one.
Specifically, I have over 800 images that are supposed to load in the Modal. But, right now, all those images are loading at once, causing a long loading time.
Here's what I currently have:
new.html.erb
# basic html items
<%= render "image_modal" %>
_image_modal.html.erb
<div class="modal fade" id="choosePicture" 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">Choose Your Image</h4>
</div>
<div class="modal-body">
<div class="row">
<% #images_by_filename.each do |image| %>
<div class=" col-xs-3">
<div class="thumbnail">
<%= image_tag image.picture(:square), :title => image.name, :id => image.id, :image_url => image.picture(:thumb), :class => "list_thumb image-select style_image" %>
</div>
</div>
<% end %>
</div>
</div>
<div class="modal-footer">
<div class="form-group">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div><!-- /.modal-content -->
images_controller.rb
class ImagesController < ApplicationController
def new
#template = Template.new
#groups = Group.all
#images = #images_by_filename
#template.template_assignments.build
end
end
How could I incorporate Kaminari to work within this modal, separately? When I try to implement the pagination, it ends up refreshing the entire page, as opposed to just within the modal.
Any help on this would be great!
Some code is missing in controller and view files.
images_controller.rb
#images = #images_by_filename.page(params[:page]).per(number_of_records_per_page)
_image_modal.html.erb
= paginate #images
You can refer http://railscasts.com/episodes/254-pagination-with-kaminari for more help.
Hope this will help you.

Rails display bootstrap modal of index from another model

I have two models groups and shots. From my groups #show page, I want a modal window that will allow me to display a list of all my shots. Here is the relevant code...
app/views/groups/show.html.erb
<%= link_to "Add Exsisting Shot", shots_path, id: "shot-modal", remote: true %>
app/views/shots/modal.js.erb
$("#shot-modal").html("<%= escape_javascript(render 'modal') %>")
$("#shot-modal").modal("show")
app/controllers/shots_controller.rb
def index
#shots = Shot.all
respond_to do |format|
format.html # index.html.erb
format.js # index.html.erb
format.json { render json: #shots }
end
end
When I click the 'Add Existing Shot' link, I do a a model that sort of turns the who screen gray, but I am not getting any content. I can click anywhere on the screen, it it brings me back to my show page. I'm also not getting any errors in the console.
EDIT
app/views/shots/_modal.html.erb
<div class="modal fade" id="shot-modal" tabindex="-1" role="dialog" aria-labelledby="shot-modal-label" 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="shot-modal-label">Modal title</h4>
</div>
<div class="modal-body" id="shot-modal-body">
<table>
<tr>
<th>Name</th>
<th>Comedian</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #shots.each do |shot| %>
<tr>
<td><%= shot.name %></td>
<td><%= shot.comedian.name %></td>
<td><%= image_tag shot.pic.url(:thumb) %></td>
</tr>
<% end %>
</table>
</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>
</div>
</div>
Two things:
1 - If you have a modal like this:
<div class="modal fade" id="product-modal" tabindex="-1" role="dialog" aria-labelledby="product-modal-label" 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="product-modal-label">Modal title</h4>
</div>
<div class="modal-body" id="product-modal-body">
...
</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>
</div>
</div>
Then you should not use $("#product-modal").html(... because you will overwrite a lot of the important divs in the modal. You should use $(".modal-body","#product-modal").html(... or set the id for the modal-body as above and use $("#product-modal-body").html(....
2 - The render won't work. When you run the render command like that from a view, it is looking for a partial. So your JavaScript should actually look like something like this:
$("#product-modal").html("<%= escape_javascript(render 'allshots') %>")
And then you should put the HTML for displaying your shots in app/views/shots/_allshots.html.erb.

Rails: Render partial content if parameter is present

I'm trying to conditionally render a section of a partial, if the render includes a particular parameter "has_footer".
In _modal.html.erb, I have the following:
<div id="<%= id %>" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-header">
<button type="button" class="btn-modal-close" data-dismiss="modal" aria-hidden="true">×</button>
<header><%= title %></header>
</div>
<div class="modal-body">
<%= render partial: content %>
</div>
<% if params[:has_footer] %>
<div class="modal-footer">
<button class="btn-modal-save">Save</button>
<button class="btn-modal-cancel" data-dismiss="modal" aria-hidden="true">Cancel</button>
</div>
<% end %>
</div>
And I render the partial like this:
<%= render "pages/modal", :has_footer, id: "modal-add-campaign", title: "Add Campaign", content: "pages/modal_add_campaign" %>
Basically, I want to display the div "modal-footer" only if I include the ":has_footer" parameter in my render command.
Any help, please?
Variables passed to partials are treated like local variables. So i would do something like:
<%= render :partial => "pages/modal", locals: { has_footer: true, id: "modal-add-campaign", title: "Add Campaign", content: "pages/modal_add_campaign" } %>
And check the variable like this:
if has_footer

Resources