如何使用ROR在控制器页面内设置文本区域属性

时间:2015-02-13 06:35:05

标签: ruby ruby-on-rails-3

任何人都可以解决我的小问题吗?实际上我想在页面加载和一些条件检查后设置文本区域启用。让我实际解释我的故事。

故事:

我有一个博客页面。当用户登录并进入博客页面时,应该启用文本区域来进行评论。如果用户将直接进入博客页面写评论,它将显示除非用户尚未登录,否则禁用。

我的代码段如下。

视图/人/ index.html.erb:

<div class="photo">
</div>
<div class="main-div">
<ul>
<li>HOME</li>
<a href="/persons/new"><li>SIGN UP</li></a>
<a href="/persons/login"><li>LOGIN</li></a>
<a href="/persons/myblog"><li>MY BLOG</li></a>
<li>CONTENT</li>
</ul>
</div>

视图/人/ login.html.erb:

<div class="login-div">
<%= form_for :person,:url =>{:controller =>"sessions",:action => "logincreate"} do |f| %>
<%= f.email_field :email,:class => "email_field",placeholder:"Enter your email" %><%= f.password_field :password,:class => "email_field",placeholder:"Enter your password" %><%= f.submit "LogIn",:class => "btn-div" %>
<% end %>
</div>
<div class="message">
    <% if flash[:color]== "valid" %>
        <div class="success">
            <p><%= flash[:notice]%></p>
        </div>
    <% elsif flash[:color]== "invalid"%>
        <div class="error">
            <p><%=flash[:notice]%></p>
        </div>
    <%else%>
        <div class="notice">
            <p><%=flash[:notice]%></p>
        </div>
    <%end%>
</div>
<div class="message"> 
    <% if @person.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@person.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @person.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
</div>

视图/人/ myblog.html.erb

    <div class="navigation">
<textarea id="text" style="width:400px; height:120px" placeholder="Type your comment here" disabled ></textarea>
</div>
<button type="button" id="btn" class="btn-div" >Comment</button>
<section class="maindrop">

<div class="wrapper-demo">
    <div id="dd" class="wrapper-dropdown-2" tabindex="1">Sign in with
        <ul class="dropdown">
            <li><a href="/persons/login">Login in Website</a></li>
            <li><a href="#">Google</a></li>
        </ul>
    </div>
</div>
</section>
<div class="message">
    <% if flash[:color]== "valid" %>
        <div class="success">
            <p><%= flash[:notice]%></p>
        </div>
    <% elsif flash[:color]== "invalid"%>
        <div class="error">
            <p><%=flash[:notice]%></p>
        </div>
    <%else%>
        <div class="notice">
            <p><%=flash[:notice]%></p>
        </div>
    <%end%>
</div>
<div class="message"> 
    <% if @person.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@person.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @person.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
</div>

控制器/ persons_controller.rb

  class PersonsController < ApplicationController
    def index

    end
    def new
        @person=Person.new
    end
    def login
        @person=Person.new
    end
    def myblog

    end
    def create
        @person=Person.new(params[:person])
        if @person.save
            flash[:notice]="User has created successfully.."
            flash[:color]="valid"
            redirect_to :action => 'index'
        else
            flash[:notice]="User could not create.."
            flash[:color]="invalid"
            render 'new'
        end
    end

end


controller/sessions_controller.rb

class SessionsController < ApplicationController
    def logincreate
        @person=Person.find_by_email(params[:person][:email])
        session[:userid]=@user.id
        if @person.password==params[:person][:password]
            flash[:notice]="User has logeed in"
            flash[:color]="valid"
            redirect_to :action => "myblog"
        else
            flash[:notice]="logeed in failed"
            flash[:color]="invalid"
            render 'login'
    end
end

如果有任何关于此问题的解决方案请分享。谢谢提前..

1 个答案:

答案 0 :(得分:0)

以下是您可以做的工作 -

ApplicationController中的

helper_method :current_person, :person_logged_in?

def authenticate_user!
  redirect_to login_persons_path, alert: 'Please login to continue.' unless user_logged_in? # replace login_persons_path with the correct login route path
end

def current_person
  @current_person ||= Person.find_by_id(session[:user_id])
end

def person_logged_in?
  !!current_person
end

在你的myblog.html.erb中:

<textarea id="text" style="width:400px; height:120px" placeholder="Type your comment here" <%= person_logged_in? ? 'disabled' : '' %> ></textarea>

authenticate_user!的其他控制器中使用before_filter方法检查用户是否已登录,如果不是,则将其重定向到登录页面。