Question

I'm using devise_invitable to allow for user invites.

Here is what I have in my user model:

attr_accessor :custom_message

I created a new controller called invitations_controller.rb and here is what I have inside it:

class InvitationsController < Devise::InvitationsController
    def create
         User.invite!({email: email}, current_user) do |user|
           user.message = params[:custom_message]
     end
end

And here is what is inside my new invite form:

<%= t "devise.invitations.new.header" %>

<%= simple_form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %>

<% resource.class.invite_key_fields.each do |field| -%>
  <%= f.input field  %>
<% end -%>

<%= f.input :custom_message  %>

<%= f.submit t("devise.invitations.new.submit_button") %>
<% end %>

And this is what I have in my invitation mailer:

<%= @resource.email %>

<%= @resource.custom_message %>

<%= link_to 'Accept this invitation', accept_invitation_url(@resource, :invitation_token => @token) %>

When I enter the email address, and the custom message and click send, this is what I see in the log:

Started POST "/users/invitation" for 127.0.0.1 at 2014-04-15 15:30:18 -0400
Processing by Devise::InvitationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"x4Mb6AkF1GnZ5bHlipBbLh/X/T0zcHkkkxrtkHW7Y0U=",
"user"=>{"email"=>"email@gmail.com", "custom_message"=>"Test message"}, "commit"=>"Send an invitation"}

The email sends out successfully, I see no errors, but nothing shows up were the custom message is supposed to be, the custom message is blank from within the email that is received. What is causing this? How can I fix it?

Edit: I am getting an unpermitted parameter error on the custom_message

This is what I have in my application controller:

before_filter :configure_permitted_parameters, if: :devise_controller?

 protected

  def configure_permitted_parameters
 devise_parameter_sanitizer.for(:accept_invitation) do |u| u.permit(:username,:validate_username,  :password,:password_confirmation, :invitation_token)
devise_parameter_sanitizer.for(:accept_invitation).concat [:custom_message]
 end
 end
Was it helpful?

Solution

Might be this will work.

 User.invite!({email: email, message: params[:user][:custom_message] }, current_user)

If you are getting Un permitted error, you should do something like in either application or User controller.

before_filter :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
    devise_parameter_sanitizer.for(:invite).concat [:custom_message]
end

You also have to edit invitation mailer, since you are using custom_message attribute which has no value . You should use message since you are assigning data into it. So

<%= @resource.custom_message %>

changes to

<%= @resource.message %>

OTHER TIPS

Look closely at your log, it has the answer. Your parameter value is at

params[:user][:custom_message]

not

params[:custom_message]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top