Lee Terng Gio

Joined

1,810 Experience
1 Lesson Completed
1 Question Solved

Activity

I'm creating a User, Role and UserRole. A user can create a list of roles, and from the new user form, there is a nested form which populate a list of roles created, then the user able to select a role and associated with the new user. I'm able to create a list of roles, but facing problem when creating a nested form in the new user view file.

Here are the models, kindly advise me if relationships are correct.

class User < ApplicationRecord
  has_one :user_role
  has_one :role, through: :user_role
end
class Role < ApplicationRecord
  has_many :user_role
  has_many :user, through: :user_role
end
class UserRole < ApplicationRecord
  belongs_to :user
  belongs_to :role
end

User controller. I'm not sure if my controller is correct:

def new
  @user = User.find_by_id(params[:id])
    @user = @current_user.account.users.new
  @user = @current_user.build_user_role
end

def create
    @user = User.find_by_id(params[:id])                
    @user = @current_user.account.users.create_with_password(user_params)
if @user.save
  redirect_to users_path
else
  render 'new'
end

private
def user_params
  params.require(:user).permit(:id, :email, :password, :password_confirmation, :admin, :owner, user_role_attributes: [:user_id, :role_id])
end

end

Below is new user form:

<%= form_for(@user, remote: true) do |f| %>

<%= f.text_field :email, class: "form-control", autofocus: true, autocomplete: "off" %>
<%= f.check_box :admin, class:"checkbox" %>
<%= f.check_box :owner, class:"checkbox" %>

<%= f.fields_for :user_role do |ff| %>
<!-- I don't know how to create a select dropdown to populate a list of roles that have been created -->
<% end %>             

<%= f.button "Create",  class: "btn btn-success" %>

<% end %>

I'm implementing Switchery button in my Rails app and Turbolinks is on. When I navigate to another page, and then click the back button, the button is being duplicated.

Here is my Javascript code:

$(document).on('turbolinks:load', function() {
var elem = document.querySelector('.js-switch');
var switchery = new Switchery(elem, {className:"switchery switchery-small"});
});

Html view:
<b>Assign as administrator?<span> <%= f.check_box :admin, class:"js-switch" %></span></b>

Screenshot: ![https://i.stack.imgur.com/ePJdk.png]

So how can i handle this problem ?

I'm implementing Switchery button in my Rails app and Turbolinks is on. When I navigate to another page, and then click the back button, the button is being duplicated.

Here is my Javascript code:

$(document).on('turbolinks:load', function() {
var elem = document.querySelector('.js-switch');
var switchery = new Switchery(elem, {className:"switchery switchery-small"});
});

Html view:
<b>Assign as administrator?<span> <%= f.check_box :admin, class:"js-switch" %></span></b>

Screenshot: ![https://i.stack.imgur.com/ePJdk.png]

So how can i handle this problem ?

I'm implementing Switchery button in my Rails app and Turbolinks is on. When I navigate to another page, and then click the back button, the button is being duplicated.

Here is my Javascript code:

$(document).on('turbolinks:load', function() {
var elem = document.querySelector('.js-switch');
var switchery = new Switchery(elem, {className:"switchery switchery-small"});
});

Html view:
<b>Assign as administrator?<span> <%= f.check_box :admin, class:"js-switch" %></span></b>

Screenshot: ![https://i.stack.imgur.com/ePJdk.png]

So how can i handle this problem ?

Posted in Switchery toggle button duplicated using Turbolinks

I'm implementing Switchery on my Rails app, and I'm also using Turbolinks. Everything works fine and the switchery is working except when I click to another page and then click the back button, the switchery icon actually duplicated and show twice. How do I destroy when navigating back to the page?

Here is the JavaScript code:

var ready;
ready = function() {
    var elem = document.getElementById('js-switch');
    var switchery = new Switchery(elem, {className:"switchery switchery-small"});

};
$(document).on('turbolinks:load', ready);

I'm implementing Switchery on my Rails app, and I'm also using Turbolinks. Everything works fine and the switchery is working except when I click to another page and then click the back button, the switchery icon actually duplicated and show twice. How do I destroy when navigating back to the page?

Here is the JavaScript code:

var ready;
ready = function() {
    var elem = document.getElementById('js-switch');
    var switchery = new Switchery(elem, {className:"switchery switchery-small"});

};
$(document).on('turbolinks:load', ready);

Posted in Group Chat with ActionCable: Part 5 Discussion

Hi Chris, I don't want to use Turbolinks in my app. Is there any way to replace the code?

Posted in Rails actioncable registered connection issue

I'm implementing ActionCable in my Rails app. I'm facing an issue with Registered Connection error. I'm using MongoID in my Rails app.

Here is my connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
   identified_by :current_user

def connect
  self.current_user = find_verified_user
  logger.add_tags "ActionCable", "User #{current_user.id}"
end

protected

def find_verified_user
 current_user = User.find_by(cookies.signed[:user_id])
  if current_user
    current_user
  else
    reject_unauthorized_connection
  end  
end

  end
end

Here is my logs

Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
MONGODB | [52] localhost:27017 #1 | dashtree_development.find | STARTED | {"find"=>"users", "filter"=>{"_id"=>BSON::ObjectId('5e3f6782f026752069ba5c21')}, "lsid"=>{"id"=><BSON::Binary:0x47104983206780 type=uuid data=0xac31709d0cea4a74...>}}
MONGODB | [52] localhost:27017 | dashtree_development.find | SUCCEEDED | 0.000s
[ActionCable] [User 5e3f6782f026752069ba5c21] Registered connection (#<User:0x00007f09400c49c8>)
[ActionCable] [User 5e3f6782f026752069ba5c21] ChatroomsChannel is transmitting the subscription confirmation
[ActionCable] [User 5e3f6782f026752069ba5c21] ChatroomsChannel is streaming from chatrooms:5e3f6782f026752069ba5c21

There is an issue with Registered connection (#<User:0x00007f09400c49c8>). I'm still unable to find a solution on this.

Posted in Issues with JQuery AtWho

I getting undefined value.
Is it possible you make free for this episode? Would be appreciated.

Posted in Attach image to summernote and send email

I recently implement email in my rails app. I'm using summernote to create email templates and save it. Everything works fine. I can upload image to the editor and save it to database. But when I send the email template, the image in the email that I'd received is broken. I didn't use any uploader to handle the image uploaded, the image was encoded in Base64, so how do I fix this?

My mailer.rb
def message_send(candidate_mail)

@candidate_mail = candidate_mail

mail(

  to: "#{@candidate_mail.to}",

  from: "#{@candidate_mail.from}",

  subject: "#{@candidate_mail.subject}",

  body: "#{@candidate_mail.body}",

  content_type:  "text/html",

  important: true,

  inline_css: true,


  )

end

I can send the email, the problem is the image is broken in the received email. I have seen documentation of how to add attachments to email, but that's not what I want because those are normally refer to public file or something or they hard code it. I use summernote to save the text and image, and they render properly in view, but the image shows broken when it's being sent out as an email. For your information, the image is encoded in Base 64 when save into database.

I recently tried to implement summernote to my rails app. I can send email and everything works fine. But I want to upload image and send the email, but the email received shows that the image was broken. How do I fix it?

The email that attached with uploaded image doesn't need to be saved into database. I just want to send the email.

My mailer.rb
def message_send(candidate_mail)
@candidate_mail = candidate_mail
mail(
to: "#{@candidate_mail.to}",
from: "#{@candidate_mail.from}",
subject: "#{@candidate_mail.subject}",
body: "#{@candidate_mail.body}",
content_type: "text/html",
important: true,
inline_css: true,

)
end

Posted in Braintree Error In Production Mode

I have already solved it. Thanks!

Posted in Braintree Error In Production Mode

I have already set it
def new
@plans = Braintree::Plan.all
end

But right now the issue is
018-03-28T00:36:21.534429+00:00 app[web.1]: NoMethodError (undefined method `payment_methods' for nil:NilClass

2018-03-28T00:36:21.534430+00:00 app[web.1]: Did you mean? private_methods):

2018-03-28T00:36:21.534432+00:00 app[web.1]: app/controllers/subscriptions_controller.rb:27:in `create'

Posted in Braintree Error In Production Mode

I tried to put validation in create customer, which is
        if current_user.braintree_id?
          customer = Braintree::Customer.find(current_user.braintree_id)
        else
          result = Braintree::Customer.create(
          email: current_user.company_email,
          company: current_user.company_name,
          payment_method_nonce: params[:payment_method_nonce]
          )
          if result.success?
            customer = result.customer
            current_user.update(braintree_id: customer.id)
          else
            flash[:info] = "Opps wrong"
            render 'new'
          end
        end

Now the error shows
2018-03-28T00:36:21.534429+00:00 app[web.1]: NoMethodError (undefined method `payment_methods' for nil:NilClass

2018-03-28T00:36:21.534430+00:00 app[web.1]: Did you mean? private_methods):
2018-03-28T00:36:21.534432+00:00 app[web.1]: app/controllers/subscriptions_controller.rb:27:in `create'


I really have no idea what causes this issue because everything works fine in development mode.

Posted in Braintree Error In Production Mode

Hi, I have implemented Braintree subscription payment in rails app which followed your tutorial. Everything works well in development, however when I switched to production (I have registered with Braintree and got a real account, and I change all the key in environment)

I have tried to submit an invalid card information to test the app, the page keeps showing error.

I look at the application logs and it said 
NoMethodError (undefined method `customer' for #<Braintree::ErrorResult:0x007f6ed80f1d80>):

Here's my create method, I follow your tutorial and it works fine in development
def create
        if current_user.braintree_id?
          customer = Braintree::Customer.find(current_user.braintree_id)
        else
          result = Braintree::Customer.create(
          email: current_user.company_email,
          company: current_user.company_name,
          payment_method_nonce: params[:payment_method_nonce]
          )
      
          customer = result.customer
          current_user.update(braintree_id: customer.id)

        end

    result = Braintree::Subscription.create(
      payment_method_token: customer.payment_methods.find{ |pm| pm.default? }.token,
      plan_id: params[:plan_id]
    )
    if result.success?
    result.subscription.transactions.each do |transaction|
      current_user.transactions.create(braintree_transaction_id: transaction.id,
        plan_name: params[:plan_name],
        price: transaction.amount.to_f,
        start_date: transaction.subscription_details.billing_period_start_date,
        end_date: transaction.subscription_details.billing_period_end_date,
        subscription_id: result.subscription.id
        )
        
          
    end
  
    
    current_user.update(braintree_subscription_id: result.subscription.id, next_billing_date: result.subscription.next_billing_date,
    billing_period_start_date: result.subscription.billing_period_start_date,
    billing_period_end_date: result.subscription.billing_period_end_date,
    status: result.subscription.status,
    next_billing_period_amount: result.subscription.next_billing_period_amount,
    paid_through_date: result.subscription.paid_through_date,
    plan_id: params[:plan_id],
    plan_name: params[:plan_name])
    
    flash[:info] = "You've been subscribed successfully"
    redirect_to @current_user
    else
          flash[:warning] = "Invalid card information"
          render 'new'
    end
    end

The weird thing is it doesn't render the flash warning of unsuccessful result and redirect to the original new_subscription_path, instead the website url redirect to this
https://herokuappname.herokuapp.com/subscription.1
and the page error shows
This page isn’t working herokuappname.herokuapp.com is currently unable to handle this request.
HTTP ERROR 500

So, I want to know whether it is the customer method error (which I don't think so because it doesn't have any problem in development mode) or any other problem such as why the page url so weird?

I looked at the Braintree control panel, and the reason that the subscription failed was because the bank declined the transactions due to incorrect card information, which I entered incorrect card in order to test it, if it is invalid card info, why didn't it display the flash notice and redirect back to the new_subscription_path, instead it redirects to the subscription.1 url which I have mentioned above?
I have created a message template based on the video that you've created and everything works well and I can select template and the message will show on the text area. But I have integrate SimpleMDE as the editor, once I select the template, the message won't show the text editor, the message will only show on normal text area. How do I fix this?

Posted in Message Templates In Rich Text Editor SimpleMDE

I have created a message template based on the video that you've created and everything works well and I can select template and the message will show on the text area. But I have integrate SimpleMDE as the editor, once I select the template, the message won't show the text editor, the message will only show on normal text area. How do I fix this?

Posted in Message Templates In Rich Text Editor SimpleMDE

I have created a message template based on the video that you've created and everything works well and I can select template and the message will show on the text area. But I have integrate SimpleMDE as the editor, once I select the template, the message won't show the text editor, the message will only show on normal text area. How do I fix this?
logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.