【问题标题】:Rails: TypeError: Object doesn't support this property or method [duplicate]Rails:TypeError:对象不支持此属性或方法[重复]
【发布时间】:2018-05-01 10:33:58
【问题描述】:

所以我正在尝试制作我的第一个 ruby​​ on rails 应用程序。我对 ruby​​ 和 rails 都没有任何经验。我试图找到答案,但没有成功。

我的错误是: C:/Sites/projects/test/app/views/layouts/application.html.erb 其中第 10 行提出: TypeError:对象不支持此属性或方法 提取的源代码(第 10 行附近)

我的 application.html.erb:

 <!DOCTYPE html>
<html>
  <head>
    <title>Test</title>
    <%= csrf_meta_tags %>

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.css">

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <div class="container">
  <%= yield %>
</div>
  </body>
</html>

我的宝石文件:

source 'https://rubygems.org'
    git_source(:github) do |repo_name|
      repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
      "https://github.com/#{repo_name}.git"
    end


    # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
    gem 'rails', '~> 5.1.4'
    # Use sqlite3 as the database for Active Record
    gem 'sqlite3'
    # Use Puma as the app server
    gem 'puma', '~> 3.7'
    # Use SCSS for stylesheets
    gem 'sass-rails', '~> 5.0'
    # Use Uglifier as compressor for JavaScript assets
    gem 'uglifier', '>= 1.3.0'
    # See https://github.com/rails/execjs#readme for more supported runtimes
    # gem 'therubyracer', platforms: :ruby

    # Use CoffeeScript for .coffee assets and views
    gem 'coffee-rails', '~> 4.2'
    # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
    gem 'turbolinks', '~> 5'
    # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
    gem 'jbuilder', '~> 2.5'
    # Use Redis adapter to run Action Cable in production
    # gem 'redis', '~> 3.0'
    # Use ActiveModel has_secure_password
    # gem 'bcrypt', '~> 3.1.7'

    # Use Capistrano for deployment
    # gem 'capistrano-rails', group: :development

    group :development, :test do
      # Call 'byebug' anywhere in the code to stop execution and get a debugger console
      gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
      # Adds support for Capybara system testing and selenium driver
      gem 'capybara', '~> 2.13'
      gem 'selenium-webdriver'
    end

    group :development do
      # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
      gem 'web-console', '>= 3.3.0'
    end

    # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
    gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

路线文件:

Rails.application.routes.draw do
    root to: redirect('/ideas')
  resources :ideas
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

和控制器:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
end

class IdeasController < ApplicationController
  before_action :set_idea, only: [:show, :edit, :update, :destroy]

  # GET /ideas
  # GET /ideas.json
  def index
    @ideas = Idea.all
  end

  # GET /ideas/1
  # GET /ideas/1.json
  def show
  end

  # GET /ideas/new
  def new
    @idea = Idea.new
  end

  # GET /ideas/1/edit
  def edit
  end

  # POST /ideas
  # POST /ideas.json
  def create
    @idea = Idea.new(idea_params)

    respond_to do |format|
      if @idea.save
        format.html { redirect_to @idea, notice: 'Idea was successfully created.' }
        format.json { render :show, status: :created, location: @idea }
      else
        format.html { render :new }
        format.json { render json: @idea.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /ideas/1
  # PATCH/PUT /ideas/1.json
  def update
    respond_to do |format|
      if @idea.update(idea_params)
        format.html { redirect_to @idea, notice: 'Idea was successfully updated.' }
        format.json { render :show, status: :ok, location: @idea }
      else
        format.html { render :edit }
        format.json { render json: @idea.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /ideas/1
  # DELETE /ideas/1.json
  def destroy
    @idea.destroy
    respond_to do |format|
      format.html { redirect_to ideas_url, notice: 'Idea was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_idea
      @idea = Idea.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def idea_params
      params.require(:idea).permit(:name, :description, :picture)
    end
end

我已经尝试用 application.html.erb 中的默认值替换应用程序。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    我已经标记了一个类似的问题,这可能会有所帮助 - 他们的解决方案如下:

    我从 application.js 中删除了 require_tree 并且它起作用了

    //= 需要 jquery
    //= 需要 jquery_ujs
    //= 需要 turbolinks
    // require_tree .

    这表明您的 javascript 在某处存在问题 - 如果这种方法有效,我会在那里寻找任何不妥之处,和/或将它们一一添加。

    我会尝试的另一件事是删除您的 javascript_include_tag 中的 Turbolinks 选项,看看这是否搞砸了。

    如果这有帮助,请告诉我!


    编辑:

    要直接链接答案中的其他线程,就在这里:

    TypeError: Object doesn't support this property or method

    还有几个分支:

    Rails ExecJS::ProgramError in Pages#home?

    ExecJS::RuntimeError on Windows trying to follow rubytutorial

    在那里读了很多书:)

    【讨论】:

    • 有效!非常感谢!
    • 欢迎 - 很高兴我能帮上忙 :)
    猜你喜欢
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2012-03-23
    • 2015-07-02
    • 2019-04-09
    • 2013-03-23
    • 1970-01-01
    相关资源
    最近更新 更多