Adding a custom action to Devise Registrations..Unknown Action Error - ruby-on-rails

After Googling everywhere about how I can fix this I decided to post a question. I'm trying to add a custom action "create_pro" to the Devise Registrations controller so I can create a user with additional fields (A pro user). This form is on the same page as the devise create action for a user.
I keep getting the following error after submitting the form:
Unknown action
The action 'create_pro' could not be found for
Devise::RegistrationsController
How can I fix this?
Routes
devise_for :users,:controllers => { :registrations => "registrations" }
devise_scope :user do
post "gopro", :to => "devise/registrations#create_pro"
end
Registrations Controller
class RegistrationsController < Devise::RegistrationsController
before_filter :authenticate_user!, :only => :token
def new
super
end
def create
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:notice] = "Welcome to Banking Analytics."
redirect_to '/mybank'
else
render :action => :new
end
end
def create_pro
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:notice] = "Welcome to Banking Analytics."
redirect_to '/mybank'
else
render :action => :new
end
end
end
View
<div class="signup_form_basic">
<%= simple_form_for #user, :url => url_for(:controller => 'registrations', :action => 'create_pro') do |f| %>
<%= f.error_notification %>
<%= f.input :email, :required => true, :autofocus => true, :label => 'Username ( Your Email )', :placeholder => 'Email Address' %>
<%= f.input :password, :required => true, :autofocus => true, :label => 'Password', :placeholder => 'Password' %>
<%= f.input :password_confirmation, :required => true, :autofocus => true, :label => 'Confirm Password', :placeholder => 'Password Confirmation' %>
<%= f.button :submit, "Sign Up >>", class: 'btn btn-inverse' %>
<% end %>
</div>

I don't have an answer for the 'Unknown Action error' that you are facing, but I think the current solution is sub-optimal.
Since your goal is to have the following two classes of users
Pro-users, and
Regular (Non-pro) users,
it is probably best to follow the suggestion at How To: Customize routes to user registration pages and set up parallel structures for the two types of users.
That would be much more in line with the relationship between the two types of users.

Related

Rails, keep anchor tag in devise sign_in stored_location redirect

Problem is described well here
extract:
Given I am using a Rails app with Devise authentication
And I am registered
But I am not logged in right now
When I visit the path "/path#thing"
Then I should see the sign-in form
When I sign in
Then I should be redirected to "/path#thing"
add a hidden input in your sign in page, for me it was app/views/devise/sessions/new.html.haml
and also some javascript to populate the input:
I called the input tab_hash
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
= f.input :login, :hint => true, :required => true
= f.input :password, :as => :password, :hint => false, :required => true
= f.input :tab_hash, :as => :hidden
%div.text-right
= f.button :submit, t("formtastic.actions.sign_in"), class: 'btn-primary'
.text-center
%p
= render :partial => "devise/shared/links"
:javascript
$(document).ready( function(){
$("#user_tab_hash").val(window.location.hash);
});
add attr_accessor :tab_hash to the User model
in app/controllers/application_controller.rb
add
def after_sign_in_path_for(resource)
stored = stored_location_for(resource)
if stored.present?
stored += params['user']['tab_hash'] # refs #3485 after signing in the tab was lost
end
stored || root_url
end

Show Rails Contact Form on main page

I've created a contact form in Rails 4 with this example.
But I want to display this contact form on the main/show page of my application. How can I do this?
routes.rb.
Rails.application.routes.draw do
resources :projects
resources :contacts, only: [:new, :create]
get 'welcome/index'
root 'welcome#index'
end
contacts_controller.rb
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(params[:contact])
#contact.request = request
if #contact.deliver
flash.now[:error] = nil
else
flash.now[:error] = 'Cannot send message.'
render :new
end
end
end
Thanks.
And easy and clean way would be to create a partial
_contact_form.html.erb (Partials always starts with an underscore)
.container
%h1 Contact
= simple_form_for #contact, :html => {:class => 'form-horizontal' } do |f|
= f.input :name, :required => true
= f.input :email, :required => true
= f.input :message, :as => :text, :required => false, :input_html => {:rows => 10}
.hidden
= f.input :nickname, :hint => 'Leave this field blank!'
.form-actions
= f.button :submit, 'Send message', :class=> "btn btn-primary"
Then, in your index page:
<%= render "contacts/contact_form" %>
and in your controller index action( I don't know if 'welcome/index' is on project's controller or contacts controller)
def index
#your code
#contact = Contact.new
end
Finally, you seem quite new to rails, I'd like to recommend a free Ruby on Rails Tutorial

Undefined method `model_name' in Simple_form

So i have this 2-step Twitter Sign in and at the page where the user must enter his Email i get an undefined method error.
My Form :
<%= simple_form_for(#user, :as => "user", :url => account_create_path, :html => {:class => "form-inline"}) do |f| %>
<%= f.input :email, :placeholder => User.human_attribute_name(:email), :class => "input-medium" %>
<%= f.submit "Finish registration", :class => "btn btn-small" %>
<% end %>
I think that it's a Syntax problem, but as i am fairly new to Rails i cant figure out what is wrong. Google wont tell me either :)
my Accounts_controller.rb:
def create
data = session["devise.omniauth_data"]
data[:email] = params[:user][:email]
user = User.find_for_twitter_oauth(data)
user.email = data[:email]
if user.save
flash[:notice] = I18n.t "devise.registrations.signed_up_but_unconfirmed"
redirect_to root_path
else
flash[:error] = I18n.t "devise.omniauth_callbacks.failure",
:kind => data[:provider].titleize,
:reason => user.errors.full_messages.first
render "users/omniauth_callbacks/add_email"
end
end
Change variable user to instance variable #user in controller and problem will be fixed.

Rails, Devise Invitations 406 Unacceptable error with STI

There are similar questions but I've gone over them and none of them have applied.
I am using Devise and Devise invitations. Rails 3.2.6.
The tricky thing is I have two user models. User and a single table inheritance setup where Artist inherits from User. I have two invitations controllers to allow sending invitations separately to both Users and Artists.
The issue right now is that artists are not able to accept their invitations. Upon form submit of updating their password, they get a 406 unacceptable error.
Relevant Code:
Routing:
devise_for :users,
:class_name => User,
:controllers => { :invitations => 'user/invitations' }
namespace :artist do
# Special route for devise, artists invitation
devise_scope :user do
resource :invitation, :controller => :invitations, :only => [:create, :new, :update] do
get :accept, :action => :edit
end
end
end
Artist invitations controller
class Artist::InvitationsController < Devise::InvitationsController
respond_to :html
# PUT /resource/invitation
def update
self.resource = Artist.accept_invitation!(params[resource_name])
if resource.errors.empty?
resource.pending
flash[:notice] = "Welcome, complete your artist agreement to get started"
sign_in(:user, resource)
respond_with resource, :location => after_accept_path_for(resource)
else
respond_with_navigational(resource){ render :edit }
end
end
end
Form description. edit.html.erb.
<%= form_for resource, :as => resource_name, :url => artist_invitation_path(resource_name), :html => { :method => :put } do |f| %>
<%= f.hidden_field :invitation_token %>
<%= f.label :password, class: 'control-label' %>
<%= f.password_field :password, :placeholder => 'Enter a password' %>
<%= f.label :password_confirmation, class: 'control-label' %>
<%= f.password_field :password_confirmation, :placeholder => 'Confirm your password' %>
<%= f.submit "Set my password", :'data-disable-with' => "Hang on..." %>
<% end %>
Accept headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:236
Content-Type:application/x-www-form-urlencoded
Returns
Request URL:http://localhost:3000/artist/invitation.user
Request Method:POST
Status Code:406 Not Acceptable
I can see that something in the format must be off because of the response adding in that .user to the end. I can not for the life of me see why or where that is happening. Please let me know if there's any info I left out which could help.
Got it, finally.
The key is to look precisely at what resource_name is giving you throughout the form and controller actions. In my case it continued to fallback to :user when it needed to be :artist and as Frost mentioned in a comment, the generated form html changes greatly if you pay attention to where resource_name is used. The update action in the controller needed to be overridden to use :artist directly and the form_for needed to be tweaked to remove resource_name from arguments and the :as argument.

form validation with simple_form and html5

The easy way from our page is
welcome-controller add an email -> second_controller create an new object with the E-Mailadddress.
we have a welcome-controller that shows our welcome-page. At this page you can type an e-mailaddress which will give to an other controller. We work with simple_form
If we that this config.browser_validations = false and enter an "normal" text we get an error on the create action. In the older version, without simple_form we get an validation-error.
If we enable this we get the html5 validation. but when the browser doesn't support html5?
Our model is here
validates :owner_email,
:presence => true,
:format => { :with => /\A[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]+\z/ },
:on => :create
Our welcome-view is here
<p>
<h2>Create a list without registration.</h2>
<%= simple_form_for([#list], :html => {:class => 'well' }) do |f| %>
<%= f.input :owner_email, :label => false, :placeholder => 'Your Email.' %>
<%= f.button :submit, "Create", :class => "btn-primary" %>
<% end %>
</p>
Our create-action from the second controller is this
def create
# create a new list and fill it up
# with some default values
#list = List.new(
:title => "List",
:description => "Beschreibung",
:owner_email => "test#domain.com",
:admin_key => generate_admin_key)
#list.update_attributes(params[:list])
respond_with(#list) do |format|
format.html {#
redirect_to :action => "admin", :id => #list.access_key, :status => 301}
end
end
What have we to change that we get errormessages in the html4 version? can everyone help us please?
Just add a :message parameter. Unless you changed simple_form configuration, message errors should be shown on the right side of the field with errors.
validates :owner_email,
:presence => true,
:format => { :with => /\A[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]+\z/ ,
:message => 'Invalid e-mail! Please provide a valid e-mail address'},
:on => :create

Resources