I need to use a separate template for dashboard. So that
http://mysite/dashboard loads a completely different template.
So, I have created a template under layouts/dashboard_template.html.erb
My dashboard cotroller
class DashboardController < ApplicationController
def index
render template: 'layouts/dashboard_template.html.erb'
end
end
I have added following routes.rb
get 'dashboard', to: 'dashboard#index'
but, it loads the template inside application.html.erb! Not sure how to fix it. Hope it is clear. Please help.
In your dashboard controller use
class DashboardController < ApplicationController
layout :set_layout, only: [:index]
def index
# do your stuff
end
def set_layout
"layout_file_name" || 'application'
end
end
this will load your layout if present else main application layout
Related
I am running a chat application on my RoR app. I want to be able to render the user chat conversations from my layouts/application.html.erb to have the user chat conversations across all pages but the chat is only working when I run the index action method under my Users controller [class UsersController < ApplicationController]. When I have my chat on views/users/index.html.erb runs great. Just to make it available in all pages I created a render form under views/users called _chatlist.html.erb which has this line inside:
and rendered this form in my layouts/application.html.erb body as:
<%= render partial: 'users/chatlist' %>
When I do that I get this error: undefined method `any?' for nil:NilClass.
How can I solve this nil issue? Any help will be appreciated.
You are getting that error because #conversations is not initialized.
You have to write an method and call it in application_controller.rb if you want to make it available on all the pages. Your application_controller.rb should look like this.
class ApplicationController < ActionController::Base
before_filter :load_conversatios
def load_conversatios
# load you conversation here
#conversations = Conversation.all
end
end
And I think that is not a good approach to load all the conversations on all the pages. You have to make it available from ajax call.
I have a rails controller page home_controller.rb
class HomeController < ApplicationController
def index
##users = User.all
#loan_applications = LoanApplication.all
end
def button_listing_show
redirect_to lp_banker_assignments_path
end
end
And I have this Rails Slim button code
button Go to Listings onclick="<%home.button_listing_show%>"
What i want is on the onclick events the page redirects to this path.
Can someone provide an answer for me either in Slim or in Rails.
Thanks
button Go to Listings onclick="#{lp_banker_assignments_path}"
You are just generating a path with that function, so why not just interpolate to the path directly?
Docs for variable interpolation
I have 2 layout
applicattion.html.erb
login.html.erb
I use devise.
in application_controller.rb I write this code:
protect_from_forgery with: :exception
layout :another_by_method
private
def another_by_method
if user_signed_in?
"application"
else
"login"
end
end
by this code, if user don't login_in, login.html.erb show.
I want use devise sign_in form in login.html.erb, but because of this code all of the page are redirect_to login.html.
I use below code too,
<%= render partial: "devise/sessions/new"%>
but get this error:
ActionView::MissingTemplate in Devise::Sessions#new
any idea?!
You are trying to render a partial, but devise/sessions/new is not a partial.
Anyway, have you generated the views with rails generate devise:views? If so, you could do that:
= render file: 'devise/sessions/new'
Use file instead of partial, but you will need all the variables provided by Devise, as resource, resource_name, or devise_mapping.
Is there a better way of using say the blue spree theme on other non spree pages?
I'm just going through and including the files that I can and adding the HTML I can't get from the includes into a master template. Would be nice if there was a better / easier way.
After some digging I figured it out.
All you have to do is set your controller to inherit from the Spree::StoreController
so go into whatever controller you want to do this for and change this:
class FooController < ApplicationController
def index
end
end
To this:
class FooController < Spree::StoreController
def index
end
end
I'm a rails beginner learning rails 4 and I'm trying to learn by doing. I'm making a simple blog that I want some simple user authentication on. I'm trying to learn here, so I don't want to implement Devise, etc. I have a header partial that takes care of my site header and I'm trying to put a link to logout that only shows if a user is logged in. I have a simple session controller that has a new action for the signup form, a create action that sets the current user after matching the email and password and sets session[:user_id] = #current_user.id, and a destroy action that nils out the session. In my application controller I have a method like this
def logged_in?
!session[:user_id].nil?
end
In my _header.html.erb partial, I have
<% if logged_in? %>
(My link)
<% end %>
When I load the page it tells me it can't find the "logged_in?" method. Anyone know why? Thanks.
Methods on controllers are by default not exposed to the views (which your partial is part of).
2 solutions:
Create your logged_in? method as a helper method, for example in an AuthenticationHelper. Doing this you cannot access it from controllers, though.
Expose your controller method to the view using helper_method:
class ApplicationController < ActionController::Base
helper_method :logged_in?
def logged_in?
[...]
end
end
Couldn't you just use:
if #current_user