简体   繁体   English

Lynda Ruby on Rails 4必不可少的培训

[英]Lynda Ruby on Rails 4 essential training not working

On chapter 14 the instructor creates an action on a controller without a corresponding view template and it works properly while I get error "missing template". 在第14章,教师在没有相应视图模板的控制器上创建一个动作,并且当我收到错误“缺少模板”时它正常工作。 Anyone else that has gotten that far to that issue your help is appreciated. 其他任何已经深入了解该问题的人都会对你的帮助表示赞赏。 (Sorry if I am missing expected items, first time posting here.) (对不起,如果我错过了预期的项目,请第一次在这里发帖。)

Error: Template is missing Missing template access/attempt_login, application/attempt_login with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby]}. 错误:模板丢失缺少模板访问/ attempt_login,application / attempt_login,{:locale => [:en],:formats => [:html],:variants => [],:handlers => [:erb,: builder,:raw,:ruby]}。 Searched in: * "/var/www/html/railsLdapTest/app/views" * "/usr/local/rvm/gems/ruby-2.1.8/gems/devise-4.0.0/app/views" 搜索:*“/ var / www / html / railsLdapTest / app / views”*“/ usr / local / rvm / gems / ruby​​-2.1.8 / gems / devise4.0.0 / app / views”

View that calls "attempt_login": 查看调用“attempt_login”:

<% @page_title = "Admin Login" %>

<div class="login">
<%= form_tag(:action => 'attempt_login') do %>
<table>
<tr>
  <td><%= label_tag(:username) %></td>
  <td><%= text_field_tag(:username) %></td>
</tr>
<tr>
  <td><%= label_tag(:password) %></td>
  <td><%= password_field_tag(:password) %></td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td><%= submit_tag("Log In") %></td>
</tr>
</table>
<% end %>
</div>

Lynda action code: Lynda行动代码:

def attempt_login
  if params[:username].present? && params[:password].present?
    found_user = AdminUser.where(:username => params[:username]).first
    if found_user
      authorized_user = found_user.authenticate(params[:password])
    end
  end

  if authorized_user
    # mark user as logged in
    session[:user_id] = authorized_user.id
    session[:username] = authorized_user.username
    flash[:notice] = "You are now logged in."
    redirect_to(:action => 'index')
  else
    flash[:notice] = "Invalid username/password combination."
    redirect_to(:action => 'login')
  end
end

My code modified to connect to a database via LDAP: 我的代码被修改为通过LDAP连接到数据库:

def attempt_login
  if params[:username].present? && params[:password].present?
    ldap = Net::LDAP.new
    ldap.host = '172.16.15.3'
    ldap.port = 389
    ldap.auth params[:username], params[:password]

    if ldap.bind
      ldap.search(
        base: "DC=abc,DC=net",
        filter: Net::LDAP::Filter.eq( "mail", params[:username] + "@abc.net" ),
        attributes: [ 'objectGUID','cn', 'telephoneNumber' ],
        return_result:true
      ) do |entry|
        @var1 = entry.telephoneNumber
        authorized_user = entry.cn
      end

      if authorized_user
        # mark user as logged in
        session[:user_id] = authorized_user
        session[:username] = authorized_user
        flash[:notice] = "You are now logged in."
        redirect_to(:action => 'index')
      else
        flash[:notice] = "Invalid username/password combination."
        redirect_to(:action => 'login')
      end
    end
  end
end

You are going to get that error anytime you are creating actions in the controller without a corresponding view. 只要在控制器中创建操作而没有相应的视图,您就会得到该错误。 The Lynda.com instructor probably fixed that issue "off-air" instead of using it as a teachable moment. Lynda.com教练可能会将这个问题“无线”修复,而不是将其用作可教的时刻。

Please go ahead and create the corresponding view template and you will see what I mean. 请继续创建相应的视图模板,您将看到我的意思。

If there is any doubt with my answer, then please review this: http://guides.rubyonrails.org/action_view_overview.html 如果对我的回答有任何疑问,请查看以下内容: http//guides.rubyonrails.org/action_view_overview.html

In particular the part that says: 特别是说:

For each controller there is an associated directory in the app/views directory which holds the template files that make up the views associated with that controller. 对于每个控制器,app / views目录中都有一个关联目录,该目录包含组成与该控制器关联的视图的模板文件。 These files are used to display the view that results from each controller action. 这些文件用于显示每个控制器操作产生的视图。

For example, lets say I create a GuidesController: 例如,假设我创建了一个GuidesController:

class GuidesController < ApplicationController

end

I am not going to create CRUD actions in this example because this is not about data flow at all. 我不打算在此示例中创建CRUD操作,因为这根本不是数据流。

class GuidesController < ApplicationController
   def book
   end
end

The significance of this piece of the answer is that you have to map it to a view directly. 这段答案的意义在于你必须直接将它映射到一个视图。 So you go to views and create a directory called guides. 所以你去视图并创建一个名为指南的目录。

This is the second stage of the mapping and how ActionView is wired up. 这是映射的第二阶段以及ActionView的连接方式。

GuidesController will only work with the view inside of guides, that is the path and so that is what the controller is going to be looking for. GuidesController只能使用指南中的视图,即路径,因此控制器将要查找的内容。

This is standard Rails convention. 这是标准的Rails约定。

Now I would have to create a book.html.erb inside the views/guides directory because the way ActionView works is it takes the method of books and maps it to the book.html.erb file. 现在我必须在views / guides目录中创建一个book.html.erb,因为ActionView的工作方式是采用书籍的方法并将其映射到book.html.erb文件。

Inside of it you can put 你可以放在里面

<p>Hey, I am in the book view</p>

The last thing that needs to be done is give it a route. 最后需要做的就是给它一条路线。 So in routes.rb: 所以在routes.rb中:

get 'book' or better yet get 'guides/book'

Make sure you have previously done a rails db:create and rails db:migrate after creation of your app so that your database will be installed. 确保您在创建应用程序之前已经完成了rails db:create和rails db:migrate,以便安装数据库。

Now when you go to localhost:3000/guides/book it will render: 现在,当你去localhost:3000 /指南/预订它将呈现:

Hey, I am in the book view

So here you have our controller and it has an action name, the action name is wired up into a specific file and it has to be given the exact name. 所以这里你有我们的控制器,它有一个动作名称,动作名称连接到一个特定的文件,它必须给出确切的名称。 Technically, you can override these conventions, but that would mean writing more code and more code is rarely a good thing. 从技术上讲,您可以覆盖这些约定,但这意味着编写更多代码,更多代码很少是好事。

Also, do your homework in vetting web development courses, lots of them are really mediocre with their instruction and material covered. 另外,在审核网络开发课程时做好功课,其中许多课程的教学和材料都非常平庸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails迁移数据库不一致(Ruby on Rails 3基本培训第6章) - Ruby on Rails Migrating Databases Inconsistencies (Ruby on Rails 3 Essential Training Chapter 6) nil的未定义方法&#39;id&#39;:NilClass Ruby on Rails基本培训 - Undefined method 'id' for nil:NilClass Ruby on Rails essential training Ruby on Rails 4基本训练错误SubjectControllerController中的ActionController :: ParameterMissing - Ruby on Rails 4 Essential Training Error ActionController::ParameterMissing in SubjectsController#create Rails 4基本训练:重定向错误 - Rails 4 essential training : redirection error Ruby on Rails-Lynda-生成控制器和视图,“ Rails Generate”命令出错 - Ruby on Rails - Lynda - Generating a Controller and View, Error on 'Rails Generate' Command Lynda Rails培训中的ActiveRecord :: RecordNotFound错误(第9章-删除/销毁) - ActiveRecord::RecordNotFound Error in Lynda Rails Training (Ch 9 - Delete/Destroy) lynda.com的Kevin Skogland的《 Ruby on Rails安装》 - ruby on rails installation by kevin skogland of lynda.com Ruby On Rails-Lynda.com-上传导师文件 - Ruby On Rails - Lynda.com - Upload tutor files rake db:migrate错误1064 Lynda.com Ruby on Rails 4问题 - rake db:migrate Error 1064 Lynda.com Ruby on Rails 4 Issue Lynda Rails 3教程期间的NoMethodError - NoMethodError during Lynda Rails 3 tutorial
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM