Rails button_to a external link - ruby-on-rails

I have a form with a field of URL generated by users now I want in the show page to get access to thise external URL by clicking in a button in the normal situation where the link have a path i am doing a code like this <%= link_to 'Sign in', new_user_session_path, class: "btn sign-home-btn" %>, but here the links are generated by users and are external

Put the url address in the second argument of the link_to. Like so:
<%= link_to 'External Url', #external_url, class: "btn sign-home-btn" %>
The link_to method just creates the html for a link. The path can be whatever you want it to be.

Related

Ruby on Rails - passing parameters in link_to with if conditions?

I want to pass a parameter (for example the param is called company=heise) on a link to, but only if the param really is present.
Lets say:
I visit somesite.com and click a link that would redirect me to mysite.com/?company=heise
On mysite i have a few link_to's and I want them to pass the parameter company=heise when it's present and since it is present now because I entered the site via mysite.com/?company=heise it should do the following:
<%= link_to "This is a link", this_link_path, class: tl(this_link_path) %>
should redirect me to mysite.com/this_link/?company=heise
If company=heise is set I want to display them in the URL and I furthermore want to not display it when it's not set.
Hope I made my question clear enough
Conditionally pass a hash, containing additional params for this_link_path url helper.
<%= link_to "This is a link", this_link_path( ({company: params[:company] } if params[:company]) ), class: tl(this_link_path) %>
To be more concise you can compact the hash.
<%= link_to "This is a link", this_link_path({company: params[:company]}.compact), class: tl(this_link_path) %>
If you know that you'll need it more often, wrap this_link_path call in a custom helper. Hash can contain additional params, including ones with fixed values.
def this_link_with_additional_params_path
this_link_path({company: params[:company], name: 'test'}.compact)
end
Then in view you can use:
<%= link_to "This is a link", this_link_with_additional_params_path, class: tl(this_link_path) %>
The idea here is to create a helper method to manage the params that you want to send to this link conditionally.
# some_helper.rb
def carried_over_params(params = {})
interesting_keys = %i(company)
params.slice(*interesting_keys).compact
end
Afterwards, you should use this in the view
<%= link_to "This is a link", this_link_path(carried_over_params(params).merge(test: 'value')), class: tl(this_link_path) %>

RAILS 5: Appending an HTML anchor to rails route path helper

I have a button that directs the user to another page:
<%= link_to "Jacuzzis", applications_path, class: "dropdown-item"%>
At the moment, this will lead the user to my 'applications' page, but there are many applications on this page. So I want to append the 'jacuzzis' id to the path variable so that the browser jumps to that section upon page load.
E.g instead of a GET request to: /applications, it's /applications#jacuzzis
I guess something like <%= link_to "Jacuzzis", (applications_path + '#jacuzzis'), class: "dropdown-item"%>
You can pass the :anchor option to your path helper.
<%= link_to "Jacuzzis", applications_path(anchor: 'jacuzzis'), class: "dropdown-item" %>
Reference: https://github.com/rails/rails/blob/85b533308f5ddcb9a59853bce38a113b66b13faa/actionview/lib/action_view/helpers/url_helper.rb#L172-L175
You can use this in rails way
<%= link_to post_path(#comment.post, anchor: 'some-id'), class: "dropdown-item" %>
Hope this will help
Thanks

Form_tag link to page in another folder

I am integrating 2 rails projects using button to link the first project to the second. Now I have this code
<%= form_tag fast_url_for(' ') do %>
<%= button_to AppConfig.app_clockout %>
<% end %>
and my current directory is
/var/www/html/wizTime/wizProject/Source/project_1
but I don't know how can this redirect to the home page of another project. The directory of my second project that I want to integrate is
/var/www/html/project_2
Please give me ideas. Thank you!
If you only want a link_to and your projects will have domains later, you can just create a link like this in rails:
<%= link_to 'sec project', "http://www.rubyonrails.org/" %> which will create an ordinary html link: <a href=""http://www.rubyonrails.org/" ...>
The form usually should not link to another project?!
If after submitting the form you want to redirect the view to another project, than you can use the controller action and redirect after submit.
As the other commenter said - you can just create a link to the other domain. You should not ever rely on your directory-structure - because when you deploy, that directory structure will very likely be subtly different.
So use the domains instead.
You can even put the domains into environment variables so that you can use different domains (eg localhost:3000 vs localhost:3001) on your development machine. you'd use them like this:
<%= link_to 'My App', ENV['MY_APP_DOMAIN'] %>
<%= link_to 'My Other App', ENV['MY_OTHER_APP_DOMAIN'] %>
Then google for how to set environment variables on your local machine to set the values.
If you want them to be buttons... then you don't need to use a form. button_to creates its own form and is used exactly the same way as a link_to eg:
<%= button_to 'My App', ENV['MY_APP_DOMAIN'] %>
<%= button_to 'My Other App', ENV['MY_OTHER_APP_DOMAIN'] %>
However... you really don't need to use a button-to if you are just doing a GET for a URL like this...
(you use buttons when you need to POST data eg POSTing form data to a create action)
You can just pass in a CSS-class and style the link-to to look as though it were a button.
eg using Bootstrap classes:
<%= link_to 'My App', ENV['MY_APP_DOMAIN'], class: 'btn btn-success' %>
<%= link_to 'My Other App', ENV['MY_OTHER_APP_DOMAIN'], class: 'btn btn-danger' %>
OR similar.

rails ajax form cancel link get rid of form

If im using Ajax to render a form into a DOM element on my page, how would I handle cancel link behavior?
Assuming I have <%= f.submit %> at the bottom of the form, what would I use for a cancel link next to it?
The ideal would be to get rid of the form without submitting or redirecting.
Use whatever you prefer.
= link_to 'Cancel', '#', class: 'cancel-link'
or
= button_to 'Cancel', '#', class: 'cancel-link'

Bootstrap buttons not applying styles Ruby on Rails

I'm having a situation here with my bootstrap buttons. I cannot get the styling to apply even though the css for the theme itself is working.
Here's my code
<%= button_to 'Sign Up', class: 'btn btn-green' %>
I tried another method, but this one gives me errors since the view isn't generated yet. But, I wanted to do was add html files that I already created for another website to the project. To use them as a view.
<%= button_to("View Web Page", {action: "Web_Page_To_Be_Used"}, class: "btn btn-primary") %>
Please, show me the best method.
UPDATE:
The code below has the best format for the link. The one I used was incomplete. For the placeholder text, you have to create a new route first. And then include it into the button's link.
= link_to 'Register', new_user_registration_path, class: 'btn btn-green btn-md' %>
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
html_options is the third argument of button_to so your second example is closer, you're probably getting an error because action expects a route.
<%= button_to("View Web Page", {action: "show"}, class: "btn btn-primary") %>

Resources