I have the following Rails controller (this controller has no model):
class Admin::AdminController < ApplicationController
def login
end
end
I am not able to link to this controller action with "link_to".
Thank you!
In your routes.rb:
namespace :admin do
resources :admin do
collection do
post :login
Your link_to
<%= link_to "Anything", login_admin_admin_path %>
Try this out.
Related
I'm trying to add a new form (for a facebook invite) inside the devise invitable new view: devise/invitations/new.html.erb. I'm struggling to get the routing right. I'm using Rails 4, if it makes a difference.
view (views/devise/invitations/new.html.erb)
<%= form_tag "invite_fb_friends" do %>
...
<% end %>
controller (controllers/users/invitations_controller)
def invite_fb_friends
raise params.to_yaml # debug
end
routes
# this is probably wrong
devise_scope :user do
post 'users/invitation/invite_fb_friends', :to => 'users/invitation#invite_fb_friends'
end
Routing Error
ActionController::RoutingError (uninitialized constant Users::InvitationController)
You're very close, but I think it's just pluralization in your route that's the problem.
Note "invitationS" in the controller name.
devise_scope :user do
post 'users/invitation/invite_fb_friends' => 'users/invitations#invite_fb_friends'
end
I have a nested resource model in my Rails 3 app. It is the standard blog app with posts and comments. I have just started using jQuery etc to make my app more dynamic, I am now struggling to remove comments in the nested model with the link_to helper.
Comments Model
class Comment < ActiveRecord::Base
belongs_to :post
end
Post Model
class Post < ActiveRecord::Base
has_many :comments, :dependent => :destroy
accepts_nested_attributes_for :comments
end
In my Posts/show.html.erb I have the following bloc that displays all the comments with a link_to helper to delete the comments. This works with HTML but when I added :remote => true, it deleted the parent post instead of the comment! How can I set it up so it deletes only the comment?
<% #post.comments.each do |comment| %>
<%= comment.body %>
<%= link_to "Approve", [#post,comment], :method =>:put, :remote=>true %>
<%= link_to "Delete", [#post,comment], :method =>:delete, :remote=>true %>
<%end%>
Thanks,
I think you want to have this in your delete action of your Comment class:
def delete
...
respond_to do |format|
format.html
format.js
end
end
Then, in your views/comments directory, you should have a file named delete.js.erb that does your jQuery DOM manipulation (finds the particular comment that you clicked the delete link of and remove it.
Then, your link_to for the delete method in your Posts/show.html.erb file, you can specify the controller and action, and also pass in any data you might need (the parent post so you can refer to it in your jQuery). You can refer to the first examples section of this site for the syntax on specifying a particular controller and action for a link_to helper here.
Just check the corresponding action in the controller.
If there is a render or redirect_to just delete it
it works for me (Rails 4)
I've created a rake task in my application and now I want the task to be accesible for app users from a link on a menu, but I don't know how to invoke it from there. Something like this...?
<%= link_to t('backup'), Rake::Task['backup'].invoke %>
I tried as you said, but the next error appears:
NameError (uninitialized constant MyTasksController::Rake)
Edit answer:
I finally could do it by this way:
class MyTasksController < ApplicationController
def rake_it
system ('rake backup:db:mysql')
redirect_to :action => 'index', :controller => '/events'
end
end
You can't do it. Link_to can link to something static or controller action. So you need to create some action, where you can invoke your Rake Task.
class MyTasksController < ApplicationController
def rake_it
Rake::Task['backup'].invoke
end
end
<%= link_to t("backup"), {:controller => :my_tasks, :action => "rake_it"} %>
I have a problem with rails 3 routes.
I want add a new action called "gestion_etudiant" with a new view "gestion_etudiant.html.erb".
I have on my index page a link like this
<%= link_to "Administration", {:controller => "users", :action => "gestion_etudiant"} %>
I also try this:
<%= link_to "Administration", "/users/gestion_etudiant" %>
In my controller:
def gestion_etudiant
#users = User.find(:all)
end
but when I clic on the link, I always have this error:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with ID=gestion_etudiant
I have this in my routes file:
resources :users
And I've also try to add:
match "users/gestion_etudiant", :to => "users#gestion_etudiant"
and
resources :users, :only => [:gestion_etudiant]
But I can not access my page "gestion_etudiant.html.erb". Can anybody suggest why?
Try this:
# router:
resources :users do
get :gestion_etudiant, :on => :collection
end
# view:
link_to "Administration", gestion_etudiant_users_path
# Controller
def gestion_etudiant
#users = User.all # Don't use find with :all as it will be deprecated in rails 3.1
end
In your routes, try:
resources :users do
collection do
get 'gestion_etudiant'
end
end
You can check the routes you have in your application by running rake routes
I'm having trouble displaying my form at /users/2/friends/new. I'm receiving
undefined method `friends_path' for #<#<Class:0x21f0c14>:0x21ef364>
Here is the beginning of the form
<% form_for(#friend) do |f| %>
And the friends controller
def new
#user = User.find(params[:user_id])
#friend = #user.friends.build
end
This is the route
resources :users do
resources :friends
end
And the relevant path from "rake routes"
users/:user_id/friends/new(.:format) {:controller=>"friends", :action=>"new"}
Any help or insight is greatly appreciated. This is my first rails 3 app.
Try:
user_friends_path(#user)
It's because it's a nested resource:
http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects
Update:
As for the form, you can do:
<%= form_for [#user, #friend] do |f| %>