How To Pass An Object To A Reveal Modal Pop-Up Window? - ruby-on-rails

I'd like to use the Reveal Modal jQuery plug-in with my Rails 3.0.3 application.
What I want to do is create a child object from a parent object's index page. Right now I have links that pass the parent's id to the child controller's new method and then open the child's new.html.erb , like this:
<%= link_to 'Add an entry', new_entry_path(:course_id => course.id) %>
What I would like to do is open a modal containing a form for creating a new child object.
The Reveal plug-in requires "data-reveal-id" be present in the link tag, e.g.:
`Click Me For A Modal`
How/can I do this using Rails helper function like link_to?
Thanks!
Jim

The following will error out:
<%= f.submit :reveal-id => "myModal" %>
or
<%= link_to 'Add an entry', new_entry_path(:course_id => course.id),:reveal-id => "myModal" %>
Try the following instead:
<%= f.submit "data-reveal-id" => "myModal" %>
or
<%= link_to 'Add an entry', new_entry_path(:course_id => course.id),"data-reveal-id" => "myModal" %>
That will work.

I have used reveal recently and this is how it worked for me :-
=link_to "Sign Up", signup_path, :class=> "buttonForModal"
.Modal.reveal-modal
Things you want in your modal window goes here
I was using javascript to load the modal window
$(document).ready(function() {
$('.buttonForModal').click(function(e) {
e.preventDefault();
$('.Modal').reveal();
});
});
It is better to use js instead of "data-reveal-id"
Hope it helps!

Related

Rails, form that can work with different controllers

I want to make erb template of searchbar that will call search method of its controller. Different controllers got same method, but will have different logic. Is it possible to make form that will work with every controller just by rendering it on their views?
--- UPDATE ---
I have found that kind of solution:
<%= form_tag "#{controller_name}/search", method: 'get' do %>
<div class="input-group">
<%= text_field_tag :sub_string, '', class: 'form-control', placeholder: "Search..." %>
<span class="input-group-btn">
<%= submit_tag 'Search', class: %w(btn btn-primary) %>
</span>
</div>
<% end %>
However when I hit the submit button, it goes to search action of my current controller and it thows an error:
Couldn't find Basis with 'id'=search
it tries to call set_basis private method, even if it is not called by before_action?
Set search form's controllers name with params[:controller].
Use Rails Guide to see how to pass controller and action for a form_tag or form_for.
I think you can
create the template/partial under views/shared folder
then in controller do something like
def search
render :partial => "shared/search"
end
Ok Then add this
in your partial so you can dynamically change your controller for the form
form_tag({controller: controller.controller_name, action: "search_action"}, method: "post", class: "form")

Bootstrap Twitter Popup works only for the first record in rails

I have the following view in my Ruby on Rails application. I want to be able to display a content of a .txt File (the path is saved in <%=file.info%>) with a popup. I included already a form for a popup. The problem is in the javascript. If I place the following javascirpt before form_tag
EDIT
<script type='text/javascript'>
$(function(){
$("#blob").popover({ title: 'info' });
});
</script>
it works only for the first record, another Info-buttons do not popup. And theoretically, i have to put the javascript after <div class="my_view"> but then it does not work at all.
<%= form_tag what_to_do_files_path, method: :get do %>
<%= button_tag :class => "btn btn-primary", :name => 'pictures' do %> Analyze<% end %>
<button type="button" id="check_all" class="btn"> Check/Uncheck all</button>
<%= button_tag :class => "btn btn-warning", :name => 'delete' do %>Delete <% end %>
<% #files.each do |file| %>
<div class="my_view">
<p><td></td><%= check_box_tag "fils[]", file.id %> <%= file.name %></p>
<% laa=File.read("#{Rails.root}"+"/public"+file[info]) %>
hover for popover
SO, my question is: How can I display the .txt File for each record, saved in the database ?
Thanks in advance
Don't use an ID to tag your "blob", use a class. You are only allowed to have one instance of an ID per page.
When calling $("#blob").popover({ title: 'info' });, it's going to go for the first instance of id="blob" that it finds. If you change it to a class, you can write
$(".blob").popover({ title: 'info' });
and it should work for all items which have the class blob.

divs in button_to in ruby on rails

When I try to use button_to for link smthing - rails add to that button tag and my design is broke. How i can fix that? For example:
<%= f.submit %>
<%= button_to 'Back', resumes_path %>
I know that i can use somthing like link_to, but in this situation i need to use button_to. Thanks.
Please create any class name or div id and write button css.
<%= link_to 'Back', resumes_path , :class => "button_css" %>
and write style,
.button_css
{
// your button style...
}
Edit : Try this
<% content_tag :button :type => :submit do %>Button<% end %>
This will create a button, you can then add onclick event to redirect the page.

button with an image?

I am trying to create a button with an image. So basically, I need the button_to version of the code below :|
<%= link_to image_tag(product.image_url, :class => "img"), line_items_path(:product_id => product) %>
This is a pretty old post, but for future reference: since Rails 3.2.1 you can use button_tag instead of button_to, as the first one natively allows images:
Creates a button element that defines a submit button, reset button or a generic button which can be used in JavaScript, for example. You can use the button tag as a regular submit tag but it isn’t supported in legacy browsers. However, the button tag allows richer labels such as images and emphasis, so this helper will also accept a block.
As for your example:
<%= button_tag image_tag(product.image_url), line_items_path(:product_id => product), class: 'img' %>
I didn't test the code, but it should work. It is possible you need to declare the url with url:
This is my solution:
Use a button helper (you can use the button_to helper method):
<%= f.submit 'Save', :class => "button_with_image_save" %>
CSS:
.button_with_image_save {
background: url(../images/icons/page_save.png) #f2efa8 no-repeat 10px 6px;
text-indent:30px;
display:block;
cursor: pointer;
}
You can create a helper as button_to link -
<%= button_to product.image_url, line_items_path(:product_id => product) %>
and in application_helper
def button_to(image_path, link)
link_to (image_tag(image_path, :class => "img"), link)
end
I guess this is what you want.
The short answer is that you need to create a helper method, which is quite simple to do:
Here is a similar SO posting the explains it: Is there a way with rails form helper to produce a button tag for submit
Good luck
Image submit button:
<%= image_submit_tag("team/team1.png", class: 'image-responsive') %>
Link with image:
<%= link_to(image_tag("team/team1.png", class: 'image-responsive'), root_path, :method => :get) %>
Add the image is the folder app/assets/image
In the view
<%= image_submit_tag('nameimage.png') %>
the disadvantage is that you can not change the size with size, but you must have the image of the size you want to appear

Rails 3 submit form with link

How I can submit form with link on correct rails 3 format?
Thanks.
<%= form_for #post do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>
My code sample.
For people who came here via Google, I have an improvement on Zequez's answer. Instead of the method that he gives, add this method to the application helper instead:
def link_to_submit(*args, &block)
link_to_function (block_given? ? capture(&block) : args[0]), "$(this).closest('form').submit()", args.extract_options!
end
Then, as Zequez stated, for simple links you can just do this in your view:
<%= link_to_submit 'Submit Form' %>
...and for more complicated buttons you can pass HTML options and a block to be used inside the link. If you use Twitter Bootstrap, for example, this lets you add CSS classes, formatting and icons:
<%= link_to_submit( class: 'btn btn-primary' ) do %>
<strong>Submit</strong> the Form <i class="icon-arrow-right"></i>
<% end %>
The JQuery code will work as long as the link is a child of the form (that is, as long as link_to_submit is called from somewhere within the form_for block).
"Correct" is a tricky word in this context ;) . One could ask why you're not just taking a button element and make it look like a link?
Anyways — you can't achieve this with plain HTML (at least not to my knowledge). With a Javascript framework like e.g. jQuery you could simply do something like this:
$('a').click(function(){
$('form').submit();
return false;
});
Rails 2.3.x had a link_to_remote helper which let's you specify a :submit parameter (= DOM element's ID, default is the parent form). So you were be able to write:
link_to_remote 'submit', :url => {…}, :submit => "my_form"
But with Rails 3's push to UJS, this helper is gone.
You can add the following to the application helper:
def link_to_submit(text)
link_to_function text, "$(this).closest('form').submit()"
end
Then inside your view files you can just call
link_to_submit 'Submit Form'
And the link must be child of the form.
With jquery, this one-liner will work fine for a simple form.
<%= link_to t("translate.submit"), "#", class: "make it beautiful", :onclick=>"$('form').submit()" %>
Of course you don't really have to use jquery, just finding the dom element for your form will work fine as well.
<%= link_to t("translate.submit"), "#", class: "make it beautiful", :onclick=>"document.getElementById('your_form_id').submit()" %>
This way you don't use any ajax, just plain form submit.
In Rails 3, the link_to_remote helper is gone, but it's replaced with
link_to 'submit me', url_for(#post), {:remote => true, :class => 'submit_me'}
In your case, you likely want your form to do the AJAX, like so:
<%= form_for #post, :remote => true do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>
With a companion link:
link_to 'submit me', '#', :class => 'submit_me'
Then, in an .js file included in the page body:
$('.submit_me').click(function() {
$('form').submit();
return false;
});
The idea is that anything more complicated than turning a link or form into an ajax request should be done with the jQuery callbacks, as listed here:
https://github.com/rails/jquery-ujs/wiki/ajax
And if you want to really get into interactive AJAX requests, go here for a great 2-part article on it.

Resources