I want to change the status of hotels in my site. When user create new hotel, he have status "pending". As an administrator, I can upgrade the hotel status from pending to approved or rejected. But I can not approved of in the rejected and vice versa.
I decided to do it with three buttons in admin panel in the place where showing all hotels but this code not working.
routes.rb
HotelAdvisor::Application.routes.draw do
devise_for :admins
devise_for :users
devise_scope :admin do
get '/admin', to: 'devise/sessions#new'
end
post '/rate' => 'rater#create', :as => 'rate'
root to: 'hotels#list'
resources :hotels do
resources :comments
get 'list', on: :collection
post 'comment'
end
resources :ratings, only: :update
namespace :admin do
resources :hotels, :users
end
base_controller
class Admin::BaseController < ApplicationController
before_filter :authenticate_admin!
layout 'admin'
end
hootels_controller(in admin folder)
class Admin::HotelsController < Admin::BaseController
def index
#hotels = Hotel.all
end
def new
#hotel = Hotel.new
end
def create
#hotel = Hotel.new(hotel_params)
#hotel.user_id = current_admin.id
if #hotel.save
render :index
else
render :new
end
end
def update
#hotel = Hotel.find(params[:id])
#hotel.update_attributes(params[:hotel])
end
end
index(in /admin/hotels)
- #hotels.each do |hotel|
.ui.segment
.ui.three.column.grid
.column
.ui.large.image
=image_tag hotel.avatar_url
=link_to hotel_path(hotel), class:'blue ui corner label' do
%i.fullscreen.icon
.column
.ui.message
.header
=hotel.title
.wraper=hotel.description.truncate(300)
.column
=simple_form_for Hotel.find([hotel.id]),:method => :put do |f|
=f.hidden_field :status, value: 'approved'
=f.button :submit, 'Approved', class: 'secondary button'
%br
%hr
I don't know why, but I see this error,
Missing template hotels/update, application/update with...
I think out that in updating rails do not use the controller in the folder admin. Perhaps this is causing the error
Given you didn't implement what to be done, e.g. render, redirect, etc. rails fallbacks to the default, which is to render views with the name of the action, in this case, update.
You might want to take some action, depending on the outcome of update_attributes, for instance:
if #hotel.update_attributes(params[:hotel])
redirect_to(#hotel)
else
render(:edit)
end
You might also want to take a look at Responders to DRY your actions.
Related
I am working on a rails 3 app, and i am trying to link_to a delete path. It seems pretty straightforward, and my routes look like this
namespace :admin do
resources :users, :except => :destroy do
...
delete :delete_contacts, :on => :collection
end
end
and my controller looks like this
class Admin::UsersController < ApplicationController
...
def delete_contacts
user = User.find(params[:user_id])
user.contacts.destroy_all
redirect_to edit_admin_user_path(current_user.id)
flash[:notice] = "Successfully deleted #{user.name} contacts"
end
end
and my current link_to looks like this
<%= link_to delete_contacts_admin_users_path(user_id: #user.id), method: :delete, data: { confirm: 'Are you sure you want to delete this users Contacts?' }, remote: true do %>
<h4 style="color: #BF3430;"><i class="material-icons">delete_forever</i> Delete Contacts</h4>
<% end %>
I have tried the routes both with and without the :on => :collection, and i seem to keep getting the same error:
ERROR
Started DELETE "/admin/users/955/delete_properties/" for 127.0.0.1 at 2018-01-17 17:58:34 -0800
Processing by Admin::UsersController#delete_properties as JS
Parameters: {"id"=>"955"}
But the link that is made, /admin/users/delete_properties/?user_id=955 , seems like just the link needed?
Does anyone see what I'm doing wrong here? this is pretty straightforward, I'm not sure why it's not behaving the way i'm thinking it should. Any help is much appreciated!
Personally, I think I would have done:
namespace :admin do
resources :users, :except => :destroy do
delete :delete_contacts, :on => :member
end
end
Which would give you:
delete_contacts_admin_user DELETE /admin/users/:id/delete_contacts(.:format) admin/users#delete_contacts
Then, you could do:
<%= link_to delete_contacts_admin_user_path(#user) ... %>
My thinking is that you're working with a particular user and so using a member path instead of a collection path, IMO, seems a little more natural.
Of course, you'd have to change your action slightly:
class Admin::UsersController < ApplicationController
...
def delete_contacts
user = User.find(params[:id])
user.contacts.destroy_all
redirect_to edit_admin_user_path(current_user.id)
flash[:notice] = "Successfully deleted #{user.name} contacts"
end
...
end
That edit_admin_user_path may be munged based on this, so you might have to look at the route for that, too (and possibly others, as well).
Hi everybody I'm from the old school using rails 2.
Actually I'm using rails 4 and I'm trying to find a way to create methods on the controller without writting
On RAILS 2 used: (only needed to write the name on the controller)
#controller
def report_a
end
def report_b
end
def report_c
end
...and whatever def
#ROUTES
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
On RAILS 4
#controller
def report_a
end
def report_b
end
def report_c
end
#ROUTES
match ':controller(/:action(/:id(.:format)))', :via => [:get, :post]
The problem is when I create a view report like this: (views/reports/report_a.html.erb)
<%= form_tag :action=>"report_a" do %>
<% end %>
I get this message:
No route matches [GET] "/reports/report_a"
To resolve this issue and doing Rails instruccions works like this:
#controller
def report_a
#users= User.search(params[:name])
end
def result_report_a
#users= User.search(params[:name])
end
#view/reports/report_a.html.erb
<%= form_tag :action=>"result_report_a" do %>
<% end %>
#routes.rb
get "reports#report_a"
post "reports#result_report_a"
get "reports#report_b"
post "reports#result_report_b"
get "reports#report_c"
post "reports#result_report_c"
Also I found this better way:
#controller reports.rb
def search_report_a
report_a
render :report_a
end
def report_a
#users = User.where(:name=>params[:name])
end
def search_report_b
report_b
render :report_b
end
def report_b
#users = User.where(:address=>params[:address])
end
...
#Routes.rb
resources :users do
match 'search_report_a', :via => [:post,:get], :on => :collection
match 'search_report_b', :via => [:post,:get], :on => :collection
...
end
Is there any other way to create methods without adding all inside ROUTES.RB ?
Any suggestions or the only way is adding get and post?
Imagine a case where you have several methods.
Best approach in Rails is to use REST architecture. Your controller should be able to view, create, update and destroy some resource (of course all actions are not mandatory).
For example:
def ReportsController
def index
# Actions to show links to all possible reports
end
def show
# Show report based on params
end
end
Your #show method may show any of report (report_a, report_b, etc) just by checking param from GET request.
And you don't need to make all logics inside #show method. It would be better to place report-related logic in, maybe, some service objects.
I'm trying to make it so only admins can add uses with devise. I've gotten it mostly working however now when I'm logged in as an admin and submit the sign up form it kicks me back with the error: You are already signed in.
I've tried to follow the instructions here: http://wiki.summercode.com/rails_authentication_with_devise_and_cancan but it doesn't seem to mention this situation.
Do I need to do further overriding in the editors_controller to allow this?
Here are my routes ("editors" is the name of my user model):
devise_for :admins, :skip => [:registrations]
as :admin do
get 'admin/editors' => 'editors#index', as: :admin_editors
get 'admin/editors/new' => 'editors#new', as: :new_editor
delete 'admin/editors/:id' => 'editors#destroy', as: :destroy_editor
end
devise_for :editors, :skip => [:registrations], :controllers => { :registrations => "editors" }
and my editors_controller in "app/controllers/"
class EditorsController < Devise::RegistrationsController
before_filter :check_permissions, :only => [:new, :create, :cancel]
skip_before_filter :require_no_authentication
def dashboard
render "editors/dashboard.html.haml"
end
def index
#editors = Editor.all
respond_to do |format|
format.html
end
end
private
def check_permissions
authorize! :create, resource
end
end
EDIT
I noticed this Processing by Devise::RegistrationsController#create as HTML in the logs when I submit the form. I had suspected that perhaps the skip_before_filter :require_no_authentication wasn't being called, but assumed that because the EditorsController was inheriting from RegistrationController that before filter would work properly. Is that not the case?
You'll want to implement your own create method on EditorsController instead of inheriting that action from Devise::RegistrationsController. As you're seeing, the method in Devise::RegistrationsController will first check to see if you're already logged in and kick you back if you are. If you're not logged in it will create a User account and then log you in as that user.
You're trying to get around that problem with skip_before_filter :require_no_authentication, but it's likely that your form is POSTing to /editors instead of /admin/editors. So, you'll need to add a route that allows you to get to create on the EditorsController :
as :admin do
post 'admin/editors' => 'editors#create'
# your other :admin routes here
end
Then you'd want to implement a scaled down version of create. You probably want something kind of like this :
class EditorsController < Devise::RegistrationsController
def create
build_resource(sign_up_params)
if resource.save
redirect_to admin_editors_path
else
clean_up_passwords resource
respond_with resource
end
end
# your other methods here
end
You'll also want to make sure that the admin/editors/new template is pointing the form to the correct route ('admin/editors').
None of the googleable solutions worked when I tried them. This works
What I did was create a new action in the controller and a new route for it, and connect the links on my views that normally connect to create to now call my route and action.
But that wasn't enough. Because Devise is listening and will grab any add you try to do and validate it through it's own code. So instead I just add the new user record with a sql insert.
Add this route
post 'savenew', to: 'users#savenew'
Add this action to the user controller:
def savenew
rawsql = "insert into users (email, created_at,updated_at) values ('#{user_params[:email]}',now(), now())"
sql = rawsql
ActiveRecord::Base.connection.execute(sql)
redirect_to action: 'index''
end
View: new.html.erb
change the form_for so that submit will go to the new route and action, not the default Rails one.
<%= form_for User, :url => {:action => "savenew"} do |f| %>
Using Rails 4.2.6 here (my model is User instead of Editor). The following solution bypasses (I think) any devise actions that may interfere with new User creation by the admin:
Add this action to the Users controller:
def savenew
User.create_new_user(user_params)
redirect_to action: 'index'
end
Add this private method to the Users controller if it does not exist:
private
def user_params
params.require(:user).permit(:email, :password,
:password_confirmation)
end
Add this to config/routes.rb:
match '/savenew', to: 'users#savenew', via: :post
Add this class method to the User model:
def self.create_new_user(params)
#user = User.create!(params)
end
I don't have a separate Admin class in my application. Instead, I defined an admin attribute for Users and check for it with a :validate_admin before_action filter in the UsersController.
I wanted to be able to create a new user from the :index view, so I added a button:
<%= button_to 'New User', '/new_user', class: 'btn btn-primary',
method: :get %>
You might have to tweak the above solution if you have any after_create actions in the User model (e.g. sending a welcome email).
Orders can have many states. I would like to create named routes for those. I need the state to be passed in to the controller as a param. Here is what I was thinking, but it obviously does not work.
match "order/:state/:id" => "orders#%{state}", as: "%{state}"
So I would like order/address/17 to route to orders#address, with :state and :id being passed in as params. Likewise, order/shipping/17 would route to orders#shipping, again :state and :id would be passed in.
Here is the controller.
class OrdersController < ApplicationController
before_filter :load_order, only: [:address, :shipping, :confirmation, :receipt]
before_filter :validate_state, only: [:address, :shipping, :confirmation, :receipt]
def address
#order.build_billing_address unless #order.billing_address
#order.build_shipping_address unless #order.shipping_address
end
def shipping
#shipping_rates = #order.calculate_shipping_rates
end
def confirmation
end
def receipt
end
private
def load_order
#order = Order.find(params[:id])
end
# Check to see if the user is on the correct action
def validate_state
if params[:state]
unless params[:state] == #order.state
redirect_to eval("#{#order.state}_path(:#{#order.state},#{#order.id})")
return
end
end
end
end
Here is what we ended up going with:
routes.rb
%w(address shipping confirmation receipt).each do |state|
match "order/#{state}/:id", :to => "orders##{state}", :as => state, :state => state
end
orders_controller.rb
def validate_state
if params[:state]
unless params[:state] == #order.state
redirect_to(eval("#{#order.state}_path(#order)"))
return
end
end
end
You aren't going to be able to create dynamic named routes with that sort of syntax, but you're basically just using :state as the :action. If you replace :state with :action and specify the controller manually, it'll work. Obviously, you will have to change your code to look at params[:action] rather than params[:state] (or map that variable in a before_filter), but beyond that it should work fine.
match "order/:action/:id", :controller => "orders"
Be aware that if orders has RESTful resource mappings like create or delete, this route would allow GET requests to them, which would be bad; you may just want to add explicit routes for each action you want to complete. This will let you get params[:state], as well:
%w(address shipping).each do |state|
match "order/#{state}/:id", :to => "orders##{state}", :as => state, :state => state
end
What I want to do seems simple, but might not be "proper"
let's say I have an image resource, and I manipulate the image based on the url. In the url I want to specify it's size and whether it's grayed, colored, or dimmed or some other condition.
currently I have a number of named routes that look like this.
map.gray_product_image "images/:product/:image/gray/:size.:format", :controller => 'images', :action => 'gray_product_image'
for me the trick is that if I created this useing Rails resources, I don't know how I would specify the :size, :format, or it's "color type".
I guess I would like to add a member route and specify my params like the following.
map.resources :products do |products|
products.resources :images, :member => {:gray_product_image => {':image/:size.:format' => :get}}
end
There are other times where I have wanted to added extra info to a resource route but didn't know how.
Any help would be greatly appreciated,
Thanks.
There's no good way to remove the controller/id part of a resource. The closest you're going to get through tricking ActionController with something like this:
map.resources :gray, :path_prefix => "/images/:product/:image_id/",
:controller => 'images', :requirements => {:colour => "gray"}
Which will produce routes like www.site.com/images/product/4/gray/1234.html with the following params hash:
params => {
:image_id => 4,
:id => 1234,
:colour => "gray",
:product => "product"
}
The format won't be passed explicitly but it will be available in the controller through the usually respond_to means.
Next you'll have to work some magic in controller to trick rails into doing what you want.
class ImagesController < ApplicationController
def show
#size = params[:id]
#image = Image.find(params[:image_id])
...
end
end
This actually works better as a filter so:
class ImagesController < ApplicationController
def initialize_colour
unless params[:colour].nil?
#size = params[:id]
#colour = params[:colour]
#image = Image.find(params[:image_id])
end
end
before_filter :initialize_colour, :except => [:index, :new, :create]
...
end
However to make good use of these routes, you're going to have to pass all those extra parameters to your url for calls. Like this:
gray_url(size, :image_id => #image.id, :product => product)
But helpers make that easy.
module ApplicationHelper
def easy_gray_url(image, size, product)
gray_url(size, :image_id => image.id, :product => product)
end
end
Check out the documentation for Resources. You'll find this:
The resources method accepts the
following options to customize the
resulting routes:
:requirements - Set custom routing parameter requirements; this is a hash of either regular expressions (which must match for the route to match) or extra parameters. For example:
map.resource :profile,
:path_prefix => ':name',
:requirements => { :name => /[a-zA-Z]+/, :extra => 'value' }
will only match if the first part is alphabetic, and will pass the parameter :extra to the controller.
I have realized that the way I want to represent my resources simply falls outside of the normal Rails resources, and that's ok. The problem I was really having was that each time added anther action and named route to get to what I wanted it felt wrong, I was repeating myself, both in my routes and in my actions.
I went back to simply creating my named routes, and spent a little more time in the controller so that I could keep my routes simple. Below is what I have now, and I am ok with it.
#routes.rb
map.with_options :controller => 'sketched_images', :action => 'show', :path_prefix => '/sketches', :name_prefix => 'sketched_', :color => 'grey' do |m|
m.style "styles/:style/:color/:size.:format"
m.design "designs/:design/:color/:size.:format"
m.product "products/:product/:color/:size.:format"
m.color_combo "colored_products/:color_combo/:size.:format"
end
class SketchedImagesController < ApplicationController
caches_page :show
before_filter :load_data
def show
#size = params[:size] || 100
respond_to do |wants|
wants.png
wants.jpg
end
end
private
def load_data
case
when params[:design]
#image = ClothingDesign.from_param(params[:design]).sketched_image
greyed
when params[:style]
#image = ClothingStyle.from_param(params[:style]).sketched_image
greyed
when params[:product]
#image = Product.from_param(params[:product]).sketched_images.first
greyed
when params[:color_combo]
#color_combo = ColorCombo.find_by_id(params[:color_combo])
#object = #color_combo.colorable
if #object.active? && !#object.sketched_images.blank?
#image = #object.sketched_images.first
colored
else
#image = #product.style.sketched_image
dimmed
end
end
end
def greyed
#blank = "#FFF"
#print = "#000"
#highlight = "#666"
end
def colored
#blank = "##{#color_combo.blank_color.value}"
#print = "##{#color_combo.design_color.value}"
#highlight = "##{#color_combo.highlight_color.value}" unless #color_combo.highlight_color.blank?
end
def dimmed
#blank = "#BBB"
#print = "#000"
#highlight = "#444"
end
end