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
Related
Can i use variable for Rails link_to helper for making different link with variables?
For example,
<%= link_to users, users_path %>
I have link like this,
And i'd like to change this url with variable examples
So i changed url like this,
<%= link_to users, "#{examples}_path" %>
This not worked because of whole string change to url.
How can i change my link_to for use this with different variable for DRY codes?
What you're asking is really just how to perform dynamic method calls. In Ruby you can do it with send (and public_send):
<%= link_to users, send("#{examples}_path") %>
The difference between the two is that send will let you violate encapsulation and call private/protected methods.
You can also do it by calling call on the Method object:
<%= link_to users, method("#{examples}_path".to_sym).call %>
However you most likely don't even need this it in the first place. Just use the polymorphic routing helpers to create links from models:
# a link to show whatever resource happens to be
<%= link_to resource.name, resource %>
<%= link_to "Edit", edit_polymorphic_url(resource) %>
<%= link_to "New", new_polymorphic_url(resource_class) %>
<%= link_to "Frobnobize", polymorphic_url(resource, :frobnobize) %>
# a link to the index
<%= link_to resource_class.model_name.plural, resource_class %>
These all use a set of heuristics to figure out what the corresponing path helper is and then call it dynamically with send.
Or if you want to link to a specific controller or action just use the functionality provided by url_for:
# link to a specific controller action
<%= link_to "Baz", { controller: :foo, action: :bar } %>
# Will use the current controller
<%= link_to "Baz", { action: :bar } %>
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.
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") %>
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.
I need to have path like "#privacy" for tab plugin. Link must contain only anchor. When I use link_to 'Privacy', :anchor => 'privacy' Rails generate /privacy#privacy - link, that contains full path and anchor.
How can I told Rails to generate url without path (only anchor)?
Thanks.
Solved: link_to 'Privacy', '#privacy'
The following will create a link the way you want -
link_to "my-privacy", "#privacy"
In most browsers, the path of the current page will be prefixed, but if you check the source of the page, the following html will be seen -
my-privacy
This will most probably serve your purpose for the UI, just that you'll have to split the url at '#' using Javascript.
This will work for you
<%= link_to "title", resource_path(:anchor => "anchor") %>
No generation of routes is needed also no generator is needed.
<%= link_to "link text", "#", :id => 'your_id_here' %>
You will need the id to access the object via jQuery.
//edit
<%= link_to "link text", "/#anchor", :id => 'your_id_here' %>
Your best bet for this exact scenario is to just use <%= link_to "link text", "#anchor" %>. The anchor tag is used within url_for and doesn't really give you a clean way to just use an anchor.