lundi 2 septembre 2019

Throwing Exeption control characters are not allowed at line 1 column 1 while using Psych.load_stream, YAML.load in ruby

Psych.load, Psych.parse, Psych.parse_stream

Psych.load_stream(input) do |doc|

Ruby-on-Rails: route for path with arbitrary number of components?

In my ror application, I want to define a route that will dispatch any length path to a given handler.

I want any URL which begins with http://example.com/xyz/... to go to the same handler, with the full path being passed in as an argument.

In other words, any and all of the following URL's should go to the same handler for the xyz route ...

  • http://example.com/xyz
  • http://example.com/xyz/foo
  • http://example.com/xyz/foo/bar
  • http://example.com/xyz/quack/bark/oink
  • http://example.com/xyz/hip/po/pot/a/mus
  • http://example.com/xyz/[any arbitrary path with any number of components]

... and within the handler, I need to be able to retrieve the entire path that was submitted.

I thought that I should put something like this into config/routes.rb ...

get "xyz/[what goes here?]", to: "xyz#handler"

... but I could not figure out any way to make this work in ror. Am I out of luck?

How to remove scrollbar from tables

I am making a table that gets data from datatables in ruby on rails. I want the rows to autofit if the browser is smaller, instead of making a scrollbar on the width. The scrollbar should never come no matter what.

I tried some css like table-layout: fixed

dimanche 1 septembre 2019

how to change the text in the button after the button is clicked in rails

I initially have a button which has text "follow" after clicking it I need it to change it to "requested" or any other text. How it can be done.

samedi 31 août 2019

How to validate emails with error messages

I am trying to validate the format of users email.

I have tried this

<% if @user.errors.any? %>
<h2> Error! Invalid password or Email </h2>
<ul>
<% @users.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %> 

I experimented with this from a old ruby video but it didnt seen to pan out. this is my user.rb model

class User < ApplicationRecord
        has_secure_password
        validates :email, presence: true, uniqueness: true
        validates :password, length: { minimum: 4 }
        validates :email, format: { with: /(\A([a-z]*\s*)*\<*([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\>*\Z)/i }
end

I get this error currently

No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id] undefined method `errors' for nil:NilClass whenever the password or email makes an error from the requirements.

vendredi 30 août 2019

devise_for and devise_scope do not bypass authentication

I'm using rails 3.2.21 and currently upgrading to devise 3.5.10 (from 1.5.4). I have a number of routes defined in a devise_scope :user block.

Here's a sample route; the same thing is happening with other routes in my devise_scope and devise_for blocks:

devise_scope :user do
    get 'register/:purchase_url_path', :controller => :purchases, :action => :new_order, as: :ecommerce, :constraints => { :purchase_url_path => /[\w\.$-@!^]+/ }
end

When I make a request to a page defined by one of these routes, I am redirected to the sign_in page.

I've figured out that the reason for this is in the authenticate_user! method, where

devise_controller?

returns false. Before the upgrade this method was returning true.

Before the devise upgrade, executing 'PurchasesController.ancestors' in irb returns a list including:

Devise::Controllers::InternalHelpers,
Devise::Controllers::ScopedViews,
Devise::Controllers::SharedHelpers,

After the upgrade those items are not present.

What have I done wrong?

lundi 26 août 2019

Rails 3.2.x: how to filter long parameters from getting logged using ActionController action?

There are similar questions like this, this, and this.

None help because the goal is to prevent logging of long parameters within a specific action instead of using config.filter_parameters. Also the answer must work for Rails 3.2.x while many answers are based on Rails 5.

One answer suggests calling request.filtered_parameters inside the controller method, but calling request.filtered_parameters.delete :long_param did not stop :log_param from getting logged.