Ruby on rails link_to global class? - html

Is there a way to add class by default in a link_to helper?
This global setting should prevent me manually adding the :class option
everytime.

application_helper.rb
def nav_link(text, controller, action="index", class)
link_to_unless_current text, :id => nil,
:action => action,
:controller => controller,
:class => class
end
Views.html.erb
<%= nav_link "text", "controller", "action", "MYCLASS" %>

Related

Rails collection_select custom name attribute

I have the following collection select which acts as a filter in a Rails app.
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'} %>
<% end %>
This always generates a name attribute of the select element like name="doctor[id]" which results in the browser to ?utf8=✓&doctor%5Bid%5D=1, which is not quite readable.
How can I change the name attribute to just name = "doctor" or basically just remove the brackets from it?
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
The collection_select method contains the parameters "options" and "html_options". "options" allow you to add specific information, like {:include_blank => 'All'}, but does not replace html attributes.
You have to add the name to the next hash, like this:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'}, {:name => 'doctor'} %>
<% end %>
Have you tried:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All', :name => 'doctor'} %>
<% end %>

Controller and action within link_to image_tag don't execute controller code

My link_to looks like this:
<%= link_to image_tag(user_likes_selection.page_picture, :image_id =>
user_likes_selection.id, :controller => :preferences_controller,
:action => :checked_average_with_profile) %>
My controller, preferences_controller, has a method called checked_average_with_profile, which, as far as I can tell, is not being called when I click the image.
The html code that is generated from the link_to is
<img>
<a href="/preferences"><img action="checked_average_with_profile" alt="Soul_surfer_film"
controller="preferences_controller" height="70%" image_id="3254"
src="/assets/soul_surfer_film.jpg" width="70%" /></a>
</img>
Why isn't the controller code executed when the image is clicked?
in cases like these, it's easier to read the code if you use the block form of link_to
<%= link_to { :image_id => user_likes_selection.id, :controller => :preferences, :action => :checked_average_with_profile } do %>
<%= image_tag(user_likes_selection.page_picture %>
<% end %>
in your routes, you can also pass an as option so you can use a named route. assuming your routes looks like
match '/preferences/checked_average_with_profile/:image_id' => 'preferences#checked_average_with_profile', as: :check_average_profile
you can simplify your link using
link_to image_tag(user_likes_selection.page_picture), check_average_profile_path(user_likes_selection.id)
Here is how i do in my code.
<%=link_to(image_tag(user_likes_selection.page_picture), check_average_profile_path(user_likes_selection.id)) %>
Try:
<%= link_to image_tag(user_likes_selection.page_picture), url_for({:controller => 'preferences_controller', :action => 'checked_average_with_profile', :image_id => user_likes_selection.id}) %>
Put your paren after user_likes_selection.id, not at the end. You're mixing image tag properties with your link_to properties.
Try:
<%= link_to image_tag(user_likes_selection.page_picture, :image_id =>
user_likes_selection.id), {:controller => :preferences,
:action => :checked_average_with_profile} %>
Finally solved my problem by adding a collection with my action in resources:
resources :preferences do
collection do
get 'save_new_scores_to_profile'
get 'checked_average_with_profile'
end
end
Then, I modified my view code so that I could pass the image_id variable along to the controller.
<%= link_to image_tag(user_likes_selection.page_picture,
checked_average_with_profile_preferences_path(:image_id =>
user_likes_selection.id) %>
In my controller, I made sure to grab the image_id with params and put a redirect_to at the end:
def checked_average_with_profile
params[:image_id]
redirect_to preferences_url
end
If you have this problem, the key parts are passing the id (whatever that may be) within parenthesis of the controller path you specify and using a COLLECTION instead of a MEMBER in your routing file.

link_to_unless_current assign class to disabled (current) link

Anyone know if you can assign a tag and class to the disabled or current link? The example below only displays as plain text in the browser for the current link.
I have a bit of rails code displaying a list of buttons for each design in the database.
<% #id_cards.each do |id| %>
<%= link_to_unless_current id.design_type, id_card_design_path(id.id), :class => 'btn' %>
<% end %>
The active links are assigned the correct class and display as buttons.
link_to_unless_current accepts a block which can be used to override the default behavior.
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_unless_current
<%=
link_to_unless_current("Comment", { :controller => "comments", :action => "new" }) do
link_to("Go back", { :controller => "posts", :action => "index" })
end
%>
In the example above it would yield the 'Go back' link if the current page was the 'new comment' page.
#James gave proper answer its just you are too young to take it right :)
<% #id_cards.each do |id| %>
<%=
link_to_unless_current(id.design_type, id_card_design_path(id.id), :class => 'btn') do
content_tag(:p, id.design_type, :class => :some_class
end
%>
<% end %>

Rails 3. how to use disable_with in button tag?

I recently started testing web_app_theme plugin. In Create button I have something like this...
<button class="button" type="submit">
<%= image_tag("web-app-theme/icons/tick.png", :alt => "#{t("web-app-theme.save", :default => "Save")}") %> <%= t("web-app-theme.save", :default => "Save") %>
</button>
How can I add :disable_with => "Processing" to this button to prevent duplicate records?
I tried t("web-app-theme.save", :default => "Save"), :disable_with => "Processing", but of course, that didn't work.
You need to use form tag helper methods - something like this should do it:
<%= button_tag :class => "button", :type => "submit", :data => { :disable_with => "Processing" } do %>
<%= image_tag("web-app-theme/icons/tick.png", :alt => "#{t("web-app-theme.save", :default => "Save")}") %>
<%= t("web-app-theme.save", :default => "Save") %>
<% end %>
Here's a link to the API docs for the button_to method: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
Updated Dec 14, 2013 for Rails 3.2 & 4 syntax.
button_tag is entirely optional. You could just as easily edit the tag in html as follows:
<button class="button" type="submit" data-disable-with="Processing">
<%= image_tag("web-app-theme/icons/tick.png", :alt => "#{t("web-app-theme.save", :default => "Save")}") %> <%= t("web-app-theme.save", :default => "Save") %>
</button>
All that :disable_with does is add a data-disable-with attribute to the element. Then the jquery-rails gem's javascript (jquery_ujs.js) does the rest of the disabling work for you.
This is assuming, of course, that you're on rails 3.0 or higher.

Link with Name Attibute in Rails

I was wondering about how i can do this in rails
because i want something like
<a href="requests/13#new">Comment!<a>
anyone knows it?
greetings
<%= link_to "Comment!", url_for(:controller => "requests", :action => "show", :id => 13, :anchor => "new") %>
If you are working with a Request object and a restful route.
<%= link_to "Comment!", request_path(#request, :anchor => "new") %>
More details are available in the link_to helper documentation.