22 Feb 2015

Simple CoffeeScript example wth Rails 4

I was searching around for a simple example to test how coffeescript is executed with Rails 4.

I found this example from Site point but it needs some updating to work with Rails 4;

The first part build the model.

http://www.sitepoint.com/building-your-first-rails-application-models/


The model has to be modified for Rails 4 and for url link to work.  I cannot take credit for the :before_save action it was answered here (stackoverflow)

1:  class Url < ActiveRecord::Base  
2:   validates :url, presence: true  
3:   before_save :ensure_protocol  
4:   private  
5:   def ensure_protocol  
6:    self.url = "http://#{url}" unless url_protocol_present?  
7:   end  
8:   def url_protocol_present?  
9:    u = URI.parse(url)  
10:    u.scheme  
11:   end  
12:  end  



The second part builds the views and controller.

www.sitepoint.com/building-your-first-rails-application-views-and-controllers/

The controller has to be modified because Rails 4 has secure params now, note the private method and its references replacing "params[:url]"

1:  class UrlsController < ApplicationController  
2:   def new  
3:    @shortened_url = Url.new  
4:   end  
5:   def create  
6:    @shortened_url = Url.new(url_params)  
7:    if @shortened_url.save  
8:     flash[:shortened_id] = @shortened_url.id  
9:     redirect_to new_url_url  
10:    else  
11:     render :action => "new"  
12:    end  
13:   end  
14:   def show  
15:    @shortened_url = Url.find(params[:id])  
16:    redirect_to @shortened_url.url  
17:   end  
18:   private  
19:   def url_params  
20:    params.require(:url).permit(:url)  
21:   end  
22:  end  



The last part creates the coffeescript.

http://www.sitepoint.com/using-coffeescript-in-rails/

 The coffeescript in Rails for reside here "~/shorty/app/assets/javascripts/shorty.coffee"

The coffeescript needs to be modified by prepending "http://" to the value which is assigned to the "current_value"
 
1:  $(document).ready ->  
2:   preview = $("#preview-url")  
3:   $('#url_url').keyup ->  
4:    current_value ='http://' + $.trim @value  
5:    if current_value is ''  
6:     preview.hide().attr 'src', ''  
7:    else  
8:     preview.show().attr 'src', current_value  


The application.rb file works with the original generated javascript markup tags.

1:  <!DOCTYPE html>  
2:  <html>  
3:  <head>  
4:   <title>Shorty</title>  
5:   <%= stylesheet_link_tag  'application', media: 'all', 'data-turbolinks-track' => true %>  
6:   <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>  
7:   <%= csrf_meta_tags %>  
8:  </head>  
9:  <body>  
10:  <% if flash[:shortened_id].present? %>  
11:   <p class='shortened-link'>  
12:    The shortened url is available <%= link_to 'here',  
13:  url_url(flash[:shortened_id]) %>.  
14:    (Right click and copy link to share it).  
15:   </p>  
16:  <% end %>  
17:  <%= yield %>  
18:  </body>  
19:  </html> 

Github repo: https://github.com/ramblingbarney/shorty

No comments:

Post a Comment