如果模型已经存在,是否可以使用设计添加身份验证?

时间:2015-05-08 08:27:37

标签: ruby-on-rails ruby ruby-on-rails-3 devise

我已经在我的Gemfile中添加了设备并进行了捆绑安装..现在我该如何为创建的sage提供身份验证?

这是我的控制器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

这是我的new.html.erb,其中创建了一个sage

<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>

这是我的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'

2 个答案:

答案 0 :(得分:0)

将以下行添加到控制器中,

before_action :authenticate_user!

或关注this

答案 1 :(得分:0)

您需要在sage模型中为该类型添加以下命令的设计:

  

rails生成设计Sage

之前确保配置设备,如果不运行:

  

rails generate devise:install

最后一次运行

  

rake db:migrate

在此之后你不需要你的控制器用于鼠尾草CRUD,设计将为你做的只是使用url&#34; sages / sign_up&#34;

如需完整指南,请按照link

进行操作