I'm working on a html slim file for a Ruby on Rails project. I want to create a button of class btn btn-primary (I'm using bootstrap) for a controller action. Name of controller is default_responses and action is edit. So I first did:
= link_to 'Test this', :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i
This would become
Test this
where 7 is the id parameter and is correct for my case. However, it is not a button at all, just an anchored tag. It also redirects me to the correct page.
Then I tried
= link_to 'Test this', :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i, class: 'btn btn-primary'
which gave me
Test this
This is still not what I want as it is not a button still.
Also tried = link_to 'Test this', :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i, :class=> 'btn btn-primary'
It returned Test this which is still wrong.
Any suggestions? Thanks.
Rails' link_to takes multiple hash options, in your example you're supplying a single hash option, which all get passed into the url_options section. You'll need to add the curly brackets ({}) around the first hash to tell ruby which option goes to which hash.
= link_to 'Test this', { :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i }, class: 'btn btn-primary'
should work. Of course, you could also use a url helper (url_for, default_responses_path, etc) instead of the first hash.
Related
I need to separate unique ids for some linking purpose, I just want to know how to make an unique id for link_to?
Like I am using:
<%= link_to "Edit", edit_question_path(question.id),method: :get, :remote => true, :id => #question.id%>
This didn't work then I tried:
<%= link_to "Edit", edit_question_path(question.id),method: :get, :remote => true, id: myBtn_<%=question.id%> %>
Nothing worked. Is there any solution?
Try with:
<%= link_to 'Edit',
edit_question_path(question.id),
method: :get,
remote: true,
id: "myBtn_#{question.id}" %>
You need to see what's the way to access a question variable, as instance or local one (I used a question, as you're doing with the path), and myBtn_<%=question.id%> won't work if you don't have defined a myBtn variable, even less if you try to include erb tags to print inside the erb open-close for the link_to.
This is weird! I have an html.erb form, and in this form I have a button that links the user to an action in another controller (finance_extensions) to print pdf. This is working great, but the thing is that I need my own action that I'm using in the current controller (parent_wise_fee_payments), so I added this same action + view file to my controller and I changed this in pay_all_fees.html.erb:
<%= link_to "► #{t('print_receipt')}",
{:controller => "finance_extensions", :action => "pay_all_fees_receipt_pdf", :id => student.id}, :target => '_blank', :class => 'user_button' %>
to this:
<%= link_to "► #{t('print_receipt')}",
{:controller => "parent_wise_fee_payments", :action => "pay_all_fees_receipt_pdf", :id => student.id}, :target => '_blank', :class => 'user_button' %>
This causes the button to disapper. I also tried adding a test action and view in my controller, and tried removing " :controller" but it didn't solve my problem.
Is this weird or am I missing something here?!
Did you add your new action to the auth file in the config??
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.
I'm geting crazy with a namespace URL that leads to incorrect action 'show' instead of 'new'.
When I'm using this URL : admin/admin_utilisateurs/new
I get this error :
Routing Error
No route matches {:action=>"show", :controller=>"admin/admin_utilisateurs"}
Try running rake routes for more information on available routes.
This is the link_to I'm using in my index page :
link_to 'Nouveau', new_admin_admin_utilisateur_path, :class => 'btn-text btn-dimensions btn-encrusted metal'
These are my rake routes :
root / welcome#index
pub_responsables GET /catalogs/managers(.:format) pub_responsables#index
POST /catalogs/managers(.:format) pub_responsables#create
new_pub_responsable GET /catalogs/managers/new(.:format) pub_responsables#new
edit_pub_responsable GET /catalogs/managers/:id/edit(.:format) pub_responsables#edit
pub_responsable GET /catalogs/managers/:id(.:format) pub_responsables#show
PUT /catalogs/managers/:id(.:format) pub_responsables#update
DELETE /catalogs/managers/:id(.:format) pub_responsables#destroy
new_admin_utilisateur_session GET /admin_utilisateurs/sign_in(.:format) devise/sessions#new
admin_utilisateur_session POST /admin_utilisateurs/sign_in(.:format) devise/sessions#create
destroy_admin_utilisateur_session DELETE /admin_utilisateurs/sign_out(.:format) devise/sessions#destroy
admin_utilisateur_password POST /admin_utilisateurs/password(.:format) devise/passwords#create
new_admin_utilisateur_password GET /admin_utilisateurs/password/new(.:format) devise/passwords#new
edit_admin_utilisateur_password GET /admin_utilisateurs/password/edit(.:format) devise/passwords#edit
PUT /admin_utilisateurs/password(.:format) devise/passwords#update
cancel_admin_utilisateur_registration GET /admin_utilisateurs/cancel(.:format) admin_utilisateurs/registrations#cancel
admin_utilisateur_registration POST /admin_utilisateurs(.:format) admin_utilisateurs/registrations#create
new_admin_utilisateur_registration GET /admin_utilisateurs/sign_up(.:format) admin_utilisateurs/registrations#new
edit_admin_utilisateur_registration GET /admin_utilisateurs/edit(.:format) admin_utilisateurs/registrations#edit
PUT /admin_utilisateurs(.:format) admin_utilisateurs/registrations#update
DELETE /admin_utilisateurs(.:format) admin_utilisateurs/registrations#destroy
admin_utilisateur_confirmation POST /admin_utilisateurs/confirmation(.:format) devise/confirmations#create
new_admin_utilisateur_confirmation GET /admin_utilisateurs/confirmation/new(.:format) devise/confirmations#new
GET /admin_utilisateurs/confirmation(.:format) devise/confirmations#show
admin_utilisateur_unlock POST /admin_utilisateurs/unlock(.:format) devise/unlocks#create
new_admin_utilisateur_unlock GET /admin_utilisateurs/unlock/new(.:format) devise/unlocks#new
GET /admin_utilisateurs/unlock(.:format) devise/unlocks#show
admin_admin_utilisateurs GET /admin/admin_utilisateurs(.:format) admin/admin_utilisateurs#index
POST /admin/admin_utilisateurs(.:format) admin/admin_utilisateurs#create
new_admin_admin_utilisateur GET /admin/admin_utilisateurs/new(.:format) admin/admin_utilisateurs#new
edit_admin_admin_utilisateur GET /admin/admin_utilisateurs/:id/edit(.:format) admin/admin_utilisateurs#edit
admin_admin_utilisateur GET /admin/admin_utilisateurs/:id(.:format) admin/admin_utilisateurs#show
PUT /admin/admin_utilisateurs/:id(.:format) admin/admin_utilisateurs#update
DELETE /admin/admin_utilisateurs/:id(.:format) admin/admin_utilisateurs#destroy
For info, I'm using Devise on a users table which I called "admin_utilisateurs".
Devise is working great with options : :database_authenticatable, :confirmable, :recoverable, :registerable, :trackable, :timeoutable, :validatable, :lockable
The point is that I setup another controller for admin purpose on the admin_utilisateurs table.
So here it is my config/routes.rb
root :to => 'welcome#index'
resources :pub_responsables, :path =>'/catalogs/managers'
devise_for :admin_utilisateurs, :controllers => {:registrations => 'admin_utilisateurs/registrations'}
namespace :admin do
resources :admin_utilisateurs
end
So my admin controller is located in app/controllers/admin/admin_utilisateurs_controller.rb
Here is the action of my 'new' controller's action :
class Admin::AdminUtilisateursController < ApplicationController
before_filter :authenticate_admin_utilisateur!
...
def new
#admin_utilisateur = AdminUtilisateur.new
respond_with(#admin_utilisateur)
end
...
end
The view for that controller are located in app/view/admin/admin_utilisateurs/
But the issue is really connected to the route because the other path for 'show', 'edit' and 'update' are working properly.
And if I delete my controller app/controllers/admin/admin_utilisateurs_controller.rb, Rails is not complaining about missing controller, she's still complaining about the "no route for show action".
I'm really lost. Could anyone advise please ?
Thanks in advance
===== UPDATE 1 =====
Here it is the log related to my URL request :
Started GET "/admin/admin_utilisateurs/new" for 127.0.0.1 at 2012-10-25 12:55:05 +0200
Processing by Admin::AdminUtilisateursController#new as HTML
Rendered shared/_main_title.html.haml (0.4ms)
Rendered admin/admin_utilisateurs/_form.html.haml (17.2ms)
Rendered admin/admin_utilisateurs/new.html.haml within layouts/application (31.7ms)
Completed 500 Internal Server Error in 45ms
ActionController::RoutingError (No route matches {:action=>"show", :controller=>"admin/admin_utilisateurs"}):
app/views/admin/admin_utilisateurs/_form.html.haml:1:in `_app_views_admin_admin_utilisateurs__form_html_haml___780348754_85631840'
app/views/admin/admin_utilisateurs/new.html.haml:9:in `_app_views_admin_admin_utilisateurs_new_html_haml__296364877_85537950'
app/controllers/admin/admin_utilisateurs_controller.rb:51:in `new'
It seems that it is properly routed but then there is a problem in the view.
I retried to remove the controller file admin_utilisateurs_controller.rb but this time restart my web server (sudo service apache2 restart) and the error was different.
I got this time a "Routing error uninitialized constant Admin::AdminUtilisateursController".
So this confrim there is an issue in my view...
digging deeper...
OK, I found the issue.
The issue was coming from my partial form view which I use for my 'edit' action and for my 'new' action : /app/views/admin/admin_utilisateurs/_form.html.haml
= form_for #admin_utilisateur, :url => admin_admin_utilisateur_path, :html => {:class => "formulaire-standard"} do |f|
= render :partial => 'shared/error_message', :locals => { :element => #admin_utilisateur, :debut_erreur => 'Cet utilisateur ne peut être enregistré'}
.groupe-champs
.champ
= f.label :nom
= f.text_field :nom, :class => 'input-width-8-col', :required => 'required'
.champ
= f.label :prenom
= f.text_field :prenom, :class => 'input-width-5-col', :required => 'required'
.champ
= f.label :telephone
= f.telephone_field :telephone, :class => 'input-width-5-col', :required => 'required'
.champ
= f.label :mobile
= f.telephone_field :mobile, :class => 'input-width-5-col'
.champ
= f.label :email
= f.email_field :email, :class => 'input-width-8-col', :required => 'required'
.groupe-champs
= render :partial => 'shared/checkboxes_admin_utilisateur', :locals => { :resource => #admin_utilisateur }
.groupe-champs
.champ-1
= f.check_box :approved
.champ-5
= f.label :approved
.checkbox-explication
= t('activerecord.attributes.admin_utilisateur.explanations.active')
.separator
.groupe-actions
= f.submit 'Enregistrer', :class => 'btn-text btn-dimensions btn-encrusted metal'
= link_to 'Annuler', admin_admin_utilisateur_path, :class => 'btn-text btn-dimensions btn-encrusted metal'
I was using the form_for tag incorrectly. Because my form is used within a namespace, I must add the namespace into its arguments, and remove the :url option because otherwise this form will only work with the 'edit' action :
= form_for [:admin, #admin_utilisateur], :html => {:class => "formulaire-standard"} do |f|
This code lets Rails guess what path to use wether it's for a new record or to edit an existing record. So I don't need to specify the :url and the :method.
The second mistake was (a classical one) the link_to at the bottom of the form.
I forgot the 's' at the end of the route helper (admin_admin_utilisateurSSSS_path):
= link_to 'Annuler', admin_admin_utilisateurs_path, :class => 'btn-text btn-dimensions btn-encrusted metal'
The moral of the story :
ALLWAYS CHECK YOUR /log/development.log FILE ! ;-)
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" %>