Activity
before_action
is definitely the way to go! Just treat it like any other method... I use this for a contact form on every page...
#controllers/application_controller.rb
before_action :set_contact
private
def set_contact
@contact = Contact.new
end
then your partial is setup as you'd expect:
#views/shared/_contact_form.html.erb
<%= form_for @contact, url: user_contact_path do |form| %>
Hah I hear ya - I've been doing web "stuff" for well over 15 years but always stuck to CMS's like Joomla or WP and never really learned how to program, only just how to hack around what someone else did to make it fit my needs... only in the past year did I pickup Rails and boy do I wish I would have started much sooner!
It's all good though, we're here now and as long as Chris keeps pumping out these awesome videos everything will be right as rain! :)
Posted in Devise: Add a select to my signup form
I'm not 100% sure what you're trying to accomplish, but you're looking at either needing to just rename the routes or you may need to override the update method.
If it's just a route renaming issue, it should be something like this:
devise_for :users, :controllers => { registrations: 'registrations', sessions: 'sessions' }, :path => '', :path_names => {:sign_in => 'login', :sign_up => 'register'}
Of course update to whatever your names are...
Lines 41 - 62 is the update method on the registrations_controller and is what you're going to have to override to redirect to a certain action given whatever conditions you have. I haven't personally done it for this scenario so this is about as far as I can help for this one.
Definitely watch Chris' video on it, I believe that's where I finally figured it all out.
The keys things that stumped me were ensuring
accepts_nested_attributes_for :company
is properly set in the User model, and then ensure your sanitizers are set correct in registrations_controller.rb or application_controller.rb (depending on your setup):
devise_parameter_sanitizer.permit(:sign_up) do |user|
user.permit(:email, :password, :password_confirmation, :is_brand)
end
Then in your sign-up form:
<%= form_for resource, as: resource_name, url: registration_path(resource_name),
html: { class: "form-horizontal", novalidate: true } do |f| %>
and just call the :is_brand as if it's a normal field..
<%= f.select(:is_brand, options_for_select(Company.is_brands.keys.to_a)) %>
You may need to change your select method - this one is based on an enum being defined in the model to define companies
I think that's all there was to it... good luck! :)
Posted in Devise: Add a select to my signup form
Sure, I'll take a look. I learn as I go so if I've encountered it before then I can probably help!
You may have an issue with Rails 5, I'm still on 4.2.3 for the project I pulled that from. I did encounter your error a lot if I didn't have the proper before_filters set... so for mine its:
before_filter :configure_permitted_parameters, :only => [:create, :update]
Posted in Devise: Add a select to my signup form
What version of devise are you using to have had to move your permitted params into your ApplicationController?
Using Devise 4.2.0 ($ gem list | grep devise
) I still have my sanitizers in controllers/registrations_controller.rb and no issue...
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) do |user|
user.permit(:first_name, :last_name, :email, :password, :password_confirmation, :avatar, :time_zone)
end
devise_parameter_sanitizer.permit(:account_update) do |user|
user.permit(:first_name, :last_name, :avatar, :email, :password, :password_confirmation, :current_password, :time_zone,
contact_attributes: [:line_1, :line_2, :city, :state, :zip, :phone])
end
end
The only thing I don't like is having to declare the sanitizer for the :sign_up action and :account_update separately - it could probably be combined but I haven't dug into it yet (maybe not though??)
I'd suggest making a branch of your project and just test it out. But from the looks of it, it seems like that gem would help you with the organization of your assets but it doesn't seem like there's any real magic going on there that you couldn't just replicate yourself and would save yourself the overhead of the gem dependency.
Read up on how rails handles layouts and the asset pipeline. Also be sure to watch Chris' video on purchased themes.
I believe you could use layouts and get away with something like this...
#application_controller.rb
class ApplicationController < ActionController::Base
layout :user_theme
def user_theme
current_user.theme
end
end
Then each layout you could adjust which css files to use.
Depending on your needs and how you have your site setup, I believe you could also just load a themed css file from the <head>
... with a <%= stylesheet_link_tag current_user.theme %>
This should get your thinker tinkering - I'm sure there's a better way and I'm not sure what effect the multi-tenancy could have on any of these ideas...!
Posted in My Development Environment Discussion
If working on OSX, check out Dash for API referencing and snippets - https://kapeli.com/dash - integrates with most major IDE's - searchable, offline mode, etc...