How to link to a class with selective text in ruby? - ruby-on-rails

So, I'm trying to make a link which has a class and some text with the given link. Here's the code -
= link_to((session[:request_id] ? 'Save & Exit': 'Exit'), "/account/#{#name}", :id => 'cancel-application', :class => "cancel-application")
What I want is that I get a link to this class called "cancel-application", which is actually a small cancel image. In front of it, I want to get "Save & Exit" or "Exit" depending on the session. The "Save & Exit" or "Exit" button should also be linked. Something like this -
[cancel button] "Exit" (where both link to the same place)
Right now, what this code does, is put the image and the text on top of each other, which makes sense. So, what I thought of doing was put the session id in a variable and then put it after the link like this -
= #exit_text = session[:request_id] ? 'Save & Exit': 'Exit'
= link_to("/account/#{#name}", :id => 'cancel-application', :class => "cancel-application") #exit_text
This doesn't work. I was wondering how I could do something like that.
EDIT - I found a solution, although I feel there can be better ones.

You can still do this with one link:
= link_to "/store/#{#name}", :id => 'cancel-application' do
.cancel-application
= session[:request_id] ? 'Save & Exit' : 'Exit'
link_to can accept the block which is captured to build link's inner html. When you pass the block, you do not pass links label.

I just used two link_to. I guess there's no way to do it in one link_to:
= link_to("", "/account/#{#name}", :id => 'cancel-application', :class => "cancel-application")
= link_to((session[:request_id] ? 'Save & Exit': 'Exit'), "/account/#{#name}", :id => 'cancel-application')

Related

simple_form select collection populated by AJAX

I thought this would be fairly easy, but I'm not finding any help by Googling.
I have a form (simple_form) with numerous inputs with select lists (collections) that are populated from the database, so many it is slowing down the initial page load. I thought I could speed it up by only populating those drop down lists as the user selects them using Ajax. Is there something built in like remote => true for the form itself? Can someone point me in the right direction?
EDIT:
I found this SO question but I cannot figure out how to implement the answer.
Currently, my form looks like this;
= simple_form_for(#account)
= f.input :account_number
= f.input :area, collection: #areas
= f.submit nil, :class => 'btn btn-primary'
Based on the answer in the linked question, I should add something like this, but of course it is not working
= simple_form_for(#account)
= f.input :account_number
= f.input :area, collection: #areas, :input_html => {"data-remote" => true, "data-url" => "/my_areas", "data-type" => :json}
= f.submit nil, :class => 'btn btn-primary'
I can think of two ways to go about this if you don't want to load the contents initially when the page loads. One way is to run a script after the DOM has loaded to change the options for the select tag and the other is to collect the options when you click on the drop-down on the select element. I might go for the first way because there wouldn't be latency when a user clicks on the select element--they wouldn't have to wait for the options to populate.
So you'd run a jQuery script on document ready that makes an AJAX call to a method in your controller, which then returns the collections you want, then you iterate through the select elements you want to change with JQuery scripts. It might look something like this.
# in view with the select options to be changed
$(document).ready(function() {
$.get(change_selects_path, function(response) {
$.each(response, function(args) {
// code for each select element to be changed
$('.class_of_select_element').html(<%= j options_from_collection_for_select(args) %>);
});
});
)};
# in controller
def change_selects
# make db calls and store in variables to feed to $.get request
end
Note that this not tested but should give you a good start towards a solution. For further info on the each loop, you can check out this documentation.
Not sure if this fits your exact use case (please clarify if not), but I also have a few collection selects that have a large amount of database rows behind them. I use the select2-rails gem to take care of this. Users can begin to type in the name and the relevant results will show up (it will also show a few initially if they don't type something).
Check it out here: https://github.com/argerim/select2-rails
Edit: For a cascading dropdown, I recommend this gem: https://github.com/ryanb/nested_form

Correct HAML for linking an image to another page in my Ruby on Rails app

Trying to figure out how to use haml "link_to" with Ruby code in order to make an image on a page link to another page on the site. If possible I'd like to keep the nav static and fade the two pages in and out via a "Forward" and "Back" image. Any ideas? Just want to get the linking right first and then can go in and figure out the JQuery. Currently have the code below...
Thanks!!
.pad-bottom50
%div{:style => "position: absolute; top: 620px; left: 830px;"}
=link_to (image_tag(#page.photos[1].image_url(:full), :id => "#fade1", :class => "animated") if #page.photos[1].image?
.pad-top20
Syntax of link_to is
=link_to link_text, link_url, options
You missed the link_url. ie. to where the user should be taken when clicking on the image.
Here is a working example
=link_to(image_tag("http://goo.gl/FZUI3"), "http://en.wikipedia.org/wiki/Nyan_Cat", :id => "#fade1", :class => "animated")
You are not specifying the src for the link.
The syntax is:
link_to "Link Text", "/path-to-link"
To put an image in there:
image_tag "path-to-image"
link_to(image_tag("path-to-image"), "/path-to-link")
This code will make an image wrapped by a link pointing to /bacon if #page.photos[1].image?
link_to(image_tag(image_tag(#page.photos[1].image_url(:full), :id => "#fade1", :class => "animated")), "/bacon", :id => "bacon", :class => "bacon") if #page.photos[1].image?
You should first look at the parameters of a link_to tag . You will get to know about the parameters and what you can do with those parameters .
check this link
api doc
You can check all paramters of a method avilable in API Ruby on Rails

simple posts list with buttons

Please help me to understand one dumb thing.
In one hand I have user with many posts, in other hand I have list with posts (every post have button) in view. On button I need to call custom method on current post with parameters. How to do this?
Try this:
link_to 'Submit', model_path(:query => "value"), :remote => true, :method => :post

Haml Hyperlink inside a label tag

Inside my .haml file, I have
= employee.label :tos_confirmation, 'I agree to the Terms of Service.', :class => 'wide'
How do i make Terms of Service a hyperlink?
= link_to employee.label(:tos_confirmation,
'I agree to the Terms of Service.',
:class => 'wide'),
'http://example.com/tos'
The answer I was looking for is like this:
%label.wide{:for => "tos_confirmation"}
I agree to the
%a{:href => "/tos"} Terms of Service
and
= succeed "." do
%a{:href => "/privacy_policy"} Privacy Policy
The only part I'm missing is the "employee" part, which is not too important.

How to apply a link to a multiline text string in HAML?

I have a multiline chunk such as this:
%li
=link_to image_tag("image.jpg", :size => "48x48", :alt => "Something", :title => "Something", :class => "some-class"), some_path
%p.testi
"Some text right here that is divided up onto multiple lines &
%br
and just won't stop running on!”
I need a link to surround both the image and the text. Presently the image link works fine, however on the text link, I believe I have to start with a link_to block, but I'm not seeing the syntax that I should follow there.
How is this correctly done so that all of the multiple lines of text also have a link applied?
In slim (wich should be similar):
= link_to some_path, class: "some-class" do
h2 = "#{some_object.title}"
p.testi
"Some text right here that is divided up onto multiple lines &
br/
and just won't stop running on!”
See Multiline string in HAML
You want to use the | at the end of each line you are wrapping.

Resources