vendredi 8 mai 2015

How can i add authentication with devise to this code?

I have added devise in my Gemfile and did bundle install..Now how can i give authentication to the sage which is created?

This is my controller sages.rb

class SagesController < ApplicationController
  def home
  end

  def about
  end

  def new
    @sage = Sage.new
  end

  def create
    @sage= Sage.new(sage_params)
    if @sage.save
      flash[:success] = 'Thanks for Joining'
      redirect_to '/thanks'
    else
      flash[:notice] = 'Please fill the form Correctly'
      render 'new'
    end
  end

  def edit
    @sage=Sage.find(params[:id])
  end

  def update
    @sage = Sage.find(params[:sage])
    if @sage.update_attributes(sage_params)
      redirect_to '/thankss'
    else
      render 'new'
     end
end


  def resources

  end

  private
  def sage_params
    params.require(:sage).permit(:Name, :Email, :Address, :Number)
  end
end

This is my new.html.erb in which a sage is created

<div class="contact" style="padding: 50px; color: orange ">

  <%= form_for @sage do |f|  %>

      <div class="form-group" >
        <h3><%= f.label :Name%></h3>
        <%= f.text_field :Name %>
        <br/>
      </div>
      <div class="form-group" >
        <h3><%= f.label :Email%></h3>
        <%= f.email_field :Email %>

      </div>
      <div class="form-group" >
        <h3><%= f.label :Address%></h3>
        <%= f.text_field :Address %>
        <br/>
      </div>
      <div class="form-group" >
        <h3> <%= f.label :Number%></h3>
        <%= f.number_field :Number %>
        <br/>
      </div>
      <br/>
      <br/>
      <div class="submit" >
        <h2><%= f.submit 'SUBMIT'%></h2>
      </div>


   <% end %>

</div>

This is my routes.rb

  get '/thanks'=> 'users#thanks'
  get '/thankss'=>'users#thankss'
  get '/thanksss'=>'users#thanksss'
  resources :sages,only: [:new,:create,:edit, :update, :delete, :destroy]
  get '/sages/edit/:id'=>'sages#edit'
  get 'sages/home'=>'sages#home'
  get 'sages/about'=>'sages#about'
  get 'sages/resources'=>'sages#resources'
  get 'sages/delete/:id'=> 'sages#delete'
  delete 'sages/delete/:id'=> 'sages#destroy'

Aucun commentaire:

Enregistrer un commentaire