如何使用 ROR 在控制器页面内设置文本区域属性 [英] How can I set text Area properties inside controller page using ROR

查看:36
本文介绍了如何使用 ROR 在控制器页面内设置文本区域属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解决我的小问题?实际上我想在页面加载并检查一些条件后设置文本区域启用.让我解释一下我真正想要的故事.

故事:

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

我的代码片段如下.

views/person/index.html.erb:

<div class="main-div"><ul><li>首页</li><a href="/persons/new"><li>注册</li></a><a href="/persons/login"><li>LOGIN</li></a><a href="/persons/myblog"><li>我的博客</li></a><li>内容</li>

views/person/login.html.erb:

<div class="message"><% if flash[:color]== "valid" %><div class="success"><p><%= flash[:notice]%</p>

<% elsif flash[:color]== "无效"%><div class="错误"><p><%=flash[:notice]%</p>

<%else%><div class="通知"><p><%=flash[:notice]%</p>

<%end%>

<div class="message"><% 如果@person.errors.any?%><div id="error_explanation"><h2><%=pluralize(@person.errors.count, "error") %>禁止保存此帖子:</h2><ul><% @person.errors.full_messages.each 做 |message|%><li><%=消息%></li><%结束%>

<%结束%>

views/person/myblog.html.erb

 

<button type="button" id="btn" class="btn-div" >评论</button><section class="maindrop"><div class="wrapper-demo"><div id="dd" class="wrapper-dropdown-2" tabindex="1">登录<ul class="下拉菜单"><li><a href="/persons/login">登录网站</a></li><li><a href="#">Google</a></li>

</节><div class="message"><% if flash[:color]== "valid" %><div class="success"><p><%= flash[:notice]%</p>

<% elsif flash[:color]== "无效"%><div class="错误"><p><%=flash[:notice]%</p>

<%else%><div class="通知"><p><%=flash[:notice]%</p>

<%end%>

<div class="message"><% 如果@person.errors.any?%><div id="error_explanation"><h2><%=pluralize(@person.errors.count, "error") %>禁止保存此帖子:</h2><ul><% @person.errors.full_messages.each 做 |message|%><li><%=消息%></li><%结束%>

<%结束%>

控制器/persons_controller.rb

 class PersonsController <应用控制器定义索引结尾定义新@person=Person.new结尾定义登录@person=Person.new结尾定义我的博客结尾定义创建@person=Person.new(params[:person])如果@person.saveflash[:notice]="用户创建成功.."flash[:color]="有效"重定向到:动作 =>'指数'别的flash[:notice]="用户无法创建.."flash[:color]="无效"呈现新"结尾结尾结尾控制器/sessions_controller.rb类 SessionsController <应用控制器定义登录创建@person=Person.find_by_email(params[:person][:email])session[:userid]=@user.id如果@person.password==params[:person][:password]flash[:notice]="用户已登录"flash[:color]="有效"重定向到:动作 =>我的博客"别的flash[:notice]="登录失败"flash[:color]="无效"呈现登录"结尾结尾

如果对此问题有任何解决方案,请与我们分享.提前致谢..

解决方案

以下是您可以做的事情 -

ApplicationController中:

helper_method :current_person, :person_logged_in?定义身份验证用户!redirect_to login_persons_path, alert: '请登录以继续.除非 user_logged_in?# 用正确的登录路由路径替换 login_persons_path结尾def current_person@current_person ||= Person.find_by_id(session[:user_id])结尾def person_logged_in?!!current_person结尾

在您的 myblog.html.erb 中:

<textarea id="text" style="width:400px; height:120px" placeholder="在此处输入您的评论" <%= person_logged_in??'禁用':''%>></textarea>

在其他控制器中使用 authenticate_user! 方法为 before_filter 检查用户是否已登录,如果未登录则重定向到登录页面.>

Can anyone resolve my small issue ?Actually i want to set Text Area enable after the page is loading and some condition checked.Let me to explain my story actually what i want.

Story:

I have a blog page .When user will be logged in and enter into the blog page then the text area should be enabled to do the comments.If user will directly get into the blog page to write the comment it will show the disabled until unless the user has not logged in.

My code snippets are as follows.

views/person/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>

views/person/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>

views/person/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>

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

If any has any solution regarding this issue Please share with .Thanks in advance..

解决方案

Here's what you can do to make this work -

in 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

in your myblog.html.erb:

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

Use authenticate_user! method in other controllers for before_filter to check if user is logged in or not and to redirect them to login page if they're not.

这篇关于如何使用 ROR 在控制器页面内设置文本区域属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆