I want my welcome controller to use a different layout:
class WelcomeController < ApplicationController
def index
if signed_in?
layout 'default'
else
layout 'welcome'
end
render 'welcome/index'
end
end
class WelcomeController < ApplicationController
def index
if signed_in?
render :layout => 'default'
else
render :layout => 'welcome'
end
end
end
Related
Hey guys I am trying to be able to add lists to my collections. I am new to rails any help would be appreciated. Currently I am trying to build a form to make a new list, but the new/create actions seem to be messed up.
The lists will end up living in the collections show view later via ajax.
Ultimately the goal is for each user to own multiple collections and in each collection there will be multiple lists, in each lists multiple items.
Collections
class CollectionsController < ApplicationController
def index
#user = User.find(current_user)
#collection = Collection.where(:user_id => current_user.id)
end
def new
#collection = Collection.new
end
def show
#collection = Collection.find(params[:id])
#list = List.all
end
def create
#collection = Collection.new(collection_params)
#collection.user_id = current_user.id
# render :text => CGI.escapeHTML(#collection.inspect)
if #collection.save
redirect_to root_path(#user)
else
render 'new'
end
end
def edit
#collection = Collection.find(params[:id])
end
def update
if #collection.update(collection_params)
redirect_to root_path(#user)
else
render 'edit'
end
end
def destroy
#collection.destroy
redirect_to root_path(#user)
end
private
def collection_params
params.require(:collection).permit(:alias, :notes, :visibility)
end
def find_collection
#collection = #user.collection.find(params[:id])
end
end
Lists
class ListsController < ApplicationController
def index
#list = List.all
end
def new
#list = List.new
end
def create
#collection = Collection.find(params[:collection_id])
#list =
#collection.lists.create(comments_params)
if #collection.lists.save
redirect_to root_path(#user)
else
render 'new'
end
end
end
Users
class UsersController < ApplicationController
def index
#user = User.find(current_user)
#collection = Collection.where(:user_id => current_user.id)
#user.collection = Collection.where(:user_id => current_user.id)
# render :text => CGI.escapeHTML(#collection.inspect)
end
end
The link I was trying
<%= link_to '<i class="fa fa-plus-square"></i> Add Subcategory'.html_safe, new_collection_list_path(#collection.id) %>
Current routes
devise_scope :user do
authenticated :user do
root 'collections#index', as: :authenticated
resources :collections do
resources :lists
end
end
Failed form_for
<%= form_for([#collection, #collection.lists.build]) do |f| %>
<% end %>
Models
Users has_many :collections
Collections belong_to :user
has_many :lists
Lists belong_to :collection
Change your ListsController new and create actions to the following:
def new
#collection = Collection.find(params[:collection_id])
end
def create
#collection = Collection.find(params[:collection_id])
#list = #collection.lists.build(params[:list])
if #list.save
redirect_to root_path(#user)
else
render 'new'
end
end
I am trying to get admins to be able to delete postings by users.
This is the code I'm using inside post.html.erb
<% if current_user.admin? %>
<%= link_to "delete", post, method: :delete %>
<% end %>
This is what I have inside the controller
class PostsController < ApplicationController
before_filter :signed_in_user
before_filter :admin_user, only: :destroy
before_filter :correct_user, only: :destroy
def destroy
#post.destroy
redirect_to root_path
end
private
def correct_user
#post = current_user.posts.find_by_id(params[:id])
redirect_to root_path if #post.nil?
end
I'm able to get it working for correct_user, but not for admin. I get this error message
undefined local variable or method `admin_user' for #<PostsController:0x5fda230>
I tried to def admin_user as user.id ==1
private
def admin_user?
current_user && current_user.id == 1
end
But I'm experiencing the same error message.
before_filter :admin_user, only: :destroy
class ApplicationController
def admin_user?
if current_user && current_user.id == 1
return true
else
redirect_to root_url, :error => "Admin only"
return false
end
end
end
Since admin_user? is called from any controller so put it in application controller
class ApplicationController < ActionController::Base
def admin_user?
unless current_user && current_user.admin?
redirect_to root_url
return false
end
end
end
class PostsController < ApplicationController
before_filter :signed_in_user
# before_filter :admin_user?, only: :destroy
before_filter :load_post, :only: :destroy
def destroy
#post.destroy
redirect_to root_url
end
private
def load_post
#post = current_user.admin? ? Post.find(params[:id]) : current_user.posts.find(params[:id])
end
end
UPD:
As I understood from comments you want users to be able to destroy their own posts and admin to be able to destroy any posts. In this case you don't have to check is user admin in before_filter, e.g. remove this line:
before_filter :admin_user?, only: :destroy
I want to change the
def url_after_create
'/'
end
of UsersController in Clearance. If I do this:
class UsersController < Clearance::UsersController
protected
def url_after_create
'/dashboard'
end
end
When I'm trying to sign up a new user, which works perfectly when not overriding, I get the following: The action 'index' could not be found for UsersController -- The action (post) is to '/users' and it seems that since the index action is not defined it fails. What should I do?
EDIT: Added code of Clearance::UsersController
class Clearance::UsersController < ApplicationController
unloadable
skip_before_filter :authorize, :only => [:new, :create]
before_filter :redirect_to_root, :only => [:new, :create], :if => :signed_in?
def new
#user = ::User.new(params[:user])
render :template => 'users/new'
end
def create
#user = ::User.new(params[:user])
if #user.save
sign_in(#user)
redirect_back_or(url_after_create)
else
flash_failure_after_create
render :template => 'users/new'
end
end
private
def flash_failure_after_create
flash.now[:notice] = translate(:bad_email_or_password,
:scope => [:clearance, :controllers, :passwords],
:default => "Must be a valid email address. Password can't be blank.")
end
def url_after_create
'/'
end
end
This line match ':controller(/:action(/:id(.:format)))' located in routes was causing the trouble. Commenting that solved it.
I have in my application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from Mongoid::Errors::DocumentNotFound, :with => :render_not_found
def render_not_found
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end
Then I call
This code working fine for example in my routes.rb:
resources :posts
The problem is that If I have a nested resource like this in routes.rb:
resources :users do
resources :posts
end
I have this in posts_controller.rb
class PostsController < ApplicationController
end
Now with this parent :users does not work!. I have that write in every actions from posts_controller.rb this nested resource the next for working fine e.g..
def show
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #post }
end
rescue
render_not_found
end
In your controller code here,
class Users::PostsController < ApplicationController
end
you have Users::Posts, but you are not specifying the location of the PostsController in the routes above.
In routes.rb:
namespace :admin do
root :controller => "base", :action => "index"
resources :products
end
The products controller inherits from the base controller:
class Admin::BaseController < ApplicationController
#layout 'admin'
def index
end
end
class Admin::ProductsController < Admin::BaseController
def index
end
end
Without "layout 'admin'", both index views render normally.
With "layout 'admin'", the admin layout is rendered, but the views are not rendered, despite WEBricks message:
Rendered admin/products/index.html.erb within layouts/admin
<%= yield %> might help there, mate