Ruby on Rails

Simple CRUD operations with file upload ruby on rails

The controller file

class WelcomeController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  def index
  	
  end
  def list
  	@tweets = Tweet.all
  end
  def save

	upload = params[:upload]
        name =  upload['datafile'].original_filename
	directory = "public/tweet_files"
	# create the file path
	path = File.join(directory, name)
        # write the file
	File.open(path, "wb") { |f| f.write(upload['datafile'].read) }

  	t = Tweet.new
	t.title = params[:title]
	t.tweet = params[:tweet]
	t.file = name
	t.save
        
        

        flash[:notice] = "Tweet saved!"
        redirect_to :action => 'list'
  end
  def new
  end
  def edit
       @tweet = Tweet.find(params[:id])
       
  end
  def delete
       t = Tweet.find(params[:id])
       t.destroy
       flash.keep[:notice] = "Tweet deleted!"
       redirect_to :action => 'list'
  end
 def update
	upload = params[:upload]
        name =  upload['datafile'].original_filename
	directory = "public/tweet_files"
	# create the file path
	path = File.join(directory, name)
        # write the file
	File.open(path, "wb") { |f| f.write(upload['datafile'].read) }

       t = Tweet.find(params[:id])
       t.title = params[:title]
       t.tweet = params[:tweet]
       t.file = name
       t.save
       flash.keep[:notice] = "Tweet updated!"
       redirect_to :action => 'list'
  end
end

view files list.html.erb

<style>
table{
border-collapse: collapse;
    font-family: "Trebuchet MS",Arial,Helvetica,sans-serif;
    width: 100%;
}
table, td, th {
    border: 1px solid green;
    text-align:center;
}

th {
    background-color: green;
    color: white;
    font-weight:bold;
}
</style>
<%= notice %><br>
<%= link_to "Add new",:controller => :welcome, :action => :new %>
<br>
<table>
<thead><tr><th>Title</th><th>Tweet</th><th>Image</th><th>Edit</th><th>Delete</th></tr></thead>
<tbody>
<% @tweets.each do |tweet| %>
<tr>
 <td><%=  tweet.title %></td>
 <td><%=  tweet.tweet %></td>
<td><%=  image_tag("/tweet_files/#{tweet.file}") %></td>
<td><%= link_to "Edit",:controller => :welcome, :action => :edit ,:id => tweet.id %> </td>
<td><%= link_to "Delete",:controller => :welcome, :action => :delete,:id => tweet.id  %> </td>
</tr>
<% end %>
</tbody>
<% if @tweets.size == 0 %>
<em>No tweets found</em>
<% end %>


edit.html.erb


<%= form_tag({controller: "welcome", action: "update"}, method: "post",multipart: true) do %>
  <%= label_tag(:tweet, "Update Tweet") %>
 <br>
 
  <%= text_field_tag "title" , @tweet.title %> 
<br>
 
  <%= text_field_tag "tweet" , @tweet.tweet %>
<br>
<p><label for="upload_file">Select File</label> : 
<%= file_field 'upload', 'datafile' %></p>
<%=  image_tag("/tweet_files/#{@tweet.file}") %>
  <%= submit_tag("update") %>
<% end %>


new.html.erb


<%= form_tag({controller: "welcome", action: "save"}, method: "post",multipart: true) do %>
  <%= label_tag(:tweet, "Add Tweet") %>
<br>
  <%= text_field_tag "title" %>
<br>
  <%= text_field_tag "tweet" %>
<br>
<p><label for="upload_file">Select File</label> : 
<%= file_field 'upload', 'datafile' %></p>
  <%= submit_tag("Save") %>
<% end %>



routes.rb

   root 'welcome#list'
   post 'welcome/save' => 'welcome#save'
   get  'welcome/new'  => 'welcome#new'
   get  'welcome/edit/:id'  => 'welcome#edit'
   get  'welcome/delete/:id'  => 'welcome#delete'
   post  'welcome/update/:id'  => 'welcome#update'

Here is how to add migrations to active records…adding column file here

$ rails generate migration add_file_to_tweets file:string
$ rake db:migrate

Here is how to create folder

cd myrailsproject
cd public
mkdir tweet_files

Setting up an application with mysql database on rails server on ubuntu 14.04

Simple steps to be followed
I assume you have installed rails, ruby gems and mysql

1) Create a new application with mysql database.

— name of application : myapplication
— db driver mysql

	$ rails new myapplication -d mysql
        

2) Run your application on rails server

CD to myapplication

	$ rails server
       

3) Create a database for the application
CD to myapplication

	$ rails db
        

Enter the password for root user

	mysql> create database mydb;
	mysql> create user abc identified by 'PASSWORD';
	mysql> grant all privileges on mydb.* to abc@localhost identified by 'PASSWORD';
	mysql>quit
	

4) Now place the username,password and database name in database.yml file under config folder of your app.
5) Goto routes.rb
— uncomment root ‘welcome#index’

6) Create a controller class welcome_controller.rb under controller folder and add an action index to it.

	class WelcomeController < ApplicationController
		def index
		end
	end
        

7) Create a view file for function index under views/welcome/index.html.erb
write

<h1>Hello Rails</h1>

to view file.
8) Now run the rails server as
CD to myapplication

   $ rails server
  

open in browser —> localhost:3000

You will see Hello Rails in browser window.