I have in my User view Index page a button_to tag as follows:
<%= button_to "Make Admin", :action => :make_admin :user => user %>
In the User controller i have:
def make_admin
#user = User.find(params[:id])
#changed_user.role = 3
#changed_user.save
end
I get a message about bad routing, but since I'm not interested in changing the view until after the action I don't know how to route this action. Where have i gone wrong?
You need to name the path in your routes:
# routes.rb
get 'your_path' => 'user#make_admin, :as => 'make_admin' # can use post too
# controller
def make_admin
# logic to make an admin
redirect_to(some_other_path, :notice => 'User was made an admin')
end
then, in your view,
button_to 'make admin', make_admin_path
You might also want to make the call remotely, but you'll need to post another question with more information in that sense
Related
I am trying to generate a url in an actionmailer template. An example if the url I want to generate is
http://0.0.0.0:3000/users/confirm/lNbQxzFukYtEEw2RMCA
Where the last segment is a hash to identify the user
However when I use this
<%= url_for(:controller => 'users', :action => 'confirm', :id => #user.confirmhash, :only_path => false) %>
It generates this
http://0.0.0.0:3000/assets?action=confirm&controller=users&id=ZOR3dNMls8533T8hJUfCJw
How can I get it to correctly format? I have no idea where 'assets' is coming from.
Is there an easier way to use named routes that I am missing?
I've found the answer. As I'm still learning I've missed the option to create a named route. So this this the path I've taken.
In config/routes.rb
match 'user/confirm/:id' => 'users#confirm', :as => :confirm_account
Then in my action mailer template I've used
<%= link_to "Confirm your account", confirm_account_url(#user.confirmhash) %>
Which passes the :id into the controller action.
I am refactoring my access_controller into a sessions_controller and can't seem to get my destroy action working properly.
Logging in seems to work fine, but I am unable to log out of a session. Here is the link I have for logging out:
<%= link_to("Logout", :controller => "sessions", :action => 'destroy') %>
routes.rb
resources :sessions
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
...
end
def destroy
session[:user_id] = nil
flash[:notice] = "You are now logged out"
redirect_to root_url
end
end
When I click "Logout" I get redirected to "/sessions/destroy" with a message of "The action 'show' could not be found for SessionsController". The destroy actions seems to want an id, but I don't need to pass in an id, I just want to run the action.
Ah, I found the answer here: http://railscasts.com/episodes/250-authentication-from-scratch
I need to set up my routes as follows:
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
resources :sessions
I'm trying to include a login (username / password) in the header of my application.html.erb. I am getting this error:
Missing partial /login with {:handlers=>[:rjs, :builder, :rhtml, :erb, :rxml], :locale=>[:en, :en], :formats=>[:html]} in view paths "/app/views"
This is happening when I make this call in my application.html.erb:
<%= render '/login' %>
'/login' is defined in my routes.rb as:
match '/login' => "sessions#new", :as => "login"
UPDATE: here is my sessions controller:
class SessionsController < ApplicationController
def create
if user = User.authenticate(params[:email], params[:password])
session[:user_id] = user.id
user.last_login = Time.now
user.save
redirect_to root_path, :notice => "login successful"
else
flash.now[:alert] = "invalid login / password combination " # don't show pass + params[:password]
#render :action => "new"
redirect_to login_path, :notice => "wrong user pass"
end
end
def destroy
reset_session
redirect_to root_path, :notice => "successfully logged out"
end
end
I have seen in other posts that this can be due to not defining a variable in a controller action, but since this is a session, and it is in the application.html.erb (application_controller.rb), I'm not sure how to do this. Anybody know how to do this? Thanks!
<%= render "sessions/login", :#user => User.new %>
will render login partial of sessions view, i.e. '_login.html.erb' in views/sessions and instantiate #user to new user so that it can be referenced directly in the partial as :
form_for #user, :url => sessions_path do |f|
f.text_field :email
Check your file extension in my case file extension was rhtml, I changed it into html.erb.
Now its working fine.
Note:
This file with rhtml extension was working fine in rails <= 3.0.10. But stopped working in rails 3.1.12. So I changed its extension as mentioned above.
I want to have mixed https/http site.
Moreover I want have redirects from https to http(ie. after user login successfully it should redirect to root page at http).
Gems like:
rack-ssl
rack-ssl-enforcer
works perfectly but only If you want to have entire site at https
"Mixed http/https" with only ssl at A, B, C actions and only http at D, E, F - dont work.
I checked solution from another SO thread:
Rails 3 SSL routing redirects from https to http
Almost works.
Its easy to write script which will change(on entire views) helper from "_path" to "_url".
But there is a problem with links like:
<%= link_to "model", some_model %>
<%= link_to "edit model", edit_mode_url(model) %>
...
There are many diffrent models and I use often "model" at iteration blocks, so solution based on 'rewrite' script will dont work with that.
Questions:
Is there a way to change behavior of <%= link_to 'model', model %> code to fix that? Is there a possibility to overwrite path helper(standard protocol will be http, on giver parameter - https)?
Or maybe there is a another solution which I have not found yet?
Edit:
I work with Rails 3.0.9.
If you would like to add https to a particular route
you can use this code
before_filter :redirect_to_https
def redirect_to_https
redirect_to :protocol => "https://" unless (request.ssl? || request.local?)
end
You can define the routes you would like to use with the before_filter action simply by doing the following
before_filter :redirect_to_https, :except => [:action1 , :action2]
before_filter :redirect_to_https, :only => [:action1 , :action2]
Use this gem:
https://github.com/retr0h/ssl_requirement
gem install ssl_requirement
Then to add ssl_required :new, :destroy #others actions
to your controllers.
If you use devise you have to overwrite each controller and specify all actions
devise_for :users, :controllers => { :confirmations => "confirmations", :omniauth_callbacks => "omniauth_callbacks", :passwords => "passwords", :registrations => "registrations", :sessions => "sessions", :unlocks => "unlocks" } do
# etc
end
It works with Rails 3.0.x
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