小编sza*_*tan的帖子

"未定义的方法`标题'为nil:NilClass"Rails指南教程

我正在研究RailsGuides教程(创建一个博客应用程序).当我运行服务器并打开时:/posts/new一切看起来都很好.但是,当我尝试创建帖子时,我收到此错误:

帖子#show中的NoMethodError

显示/home/darek/rails_projects/blog/app/views/posts/show.html.erb在行#3提出:

nil的未定义方法`title':NilClass

提取的来源(第3行):

1  <p>
2  <strong>Title:</strong>
3  <%= @post.title %>
4  </p>
5  <p>
Run Code Online (Sandbox Code Playgroud)

事实上post已创建,我可以在/ posts看到标题和内容但是当我尝试使用show特定帖子时,我收到此错误.我的第一个线索就是换线

<%= @post.title %> 
Run Code Online (Sandbox Code Playgroud)

<%= @post.try(:title) %>
Run Code Online (Sandbox Code Playgroud)

错误消失了,但问题没有解决.
当我尝试显示特定帖子时,我获得了标题,而文本形式为空.这不是我想要看到的;)

好的,这是代码

Show.html.erb

<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>

<p>
  <strong>Text:</strong>
  <%= @post.text %>
</p>


<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <p>
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </p>
  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit %>
  </p> …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

21
推荐指数
2
解决办法
3万
查看次数

rake db:设置结果为fe_sendauth没有提供密码

当我运行:rake db:setup

我得到:

fe_sendauth: no password supplied

Couldn't create database for {"adapter"=>"postfresql", "encoding"=>"unicode",
"host"=>"localhost", "pool"=>5, "username"=>"my_user", "password"=>nil,  
"database"=>"my_db_test"}

-- enable_extension("plpgqsl")
rake aborted


Tasks: TOP => db:schema:load
Run Code Online (Sandbox Code Playgroud)

我的database.yml:

connection: &connection
adapter: postgresql
encoding: unicode
host:     localhost
pool: 5
username: my_user
password:

development:
  <<: *connection
  database: my_db_development

test: &test
  <<: *connection
  database: my_db_test
Run Code Online (Sandbox Code Playgroud)

已经在这个问题中更改了我的pg_hba.conf:尝试为ror应用程序设置postgres,收到错误 - fe_sendauth:没有提供密码 但它根本没有帮助.

ruby postgresql ubuntu ruby-on-rails pg

12
推荐指数
1
解决办法
2万
查看次数

rails中的活动记录postgresql适配器错误

这是我的问题.我正在尝试创建一个博客.我运行'rails new blog'.Everythign没问题,直到我尝试'rake db:create'.它返回一个错误:

rake aborted!
Please install the postgresql adapter: `gem install activerecord-postgresql-adap
ter` (pg is not part of the bundle. Add it to Gemfile.)
Run Code Online (Sandbox Code Playgroud)

当我尝试安装pg适配器时,我得到了这个:

Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing activerecord-postgresql-adapter:
    ERROR: Failed to build gem native extension.

F:/Ruby200-x64/bin/ruby.exe extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
 checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** …
Run Code Online (Sandbox Code Playgroud)

gem ruby-on-rails ruby-on-rails-3.1

5
推荐指数
1
解决办法
2957
查看次数

路由错误没有路由匹配[GET]"/ static_pages/home",教程

当我运行服务器浏览器时显示如下:

Routing Error

No route matches [GET] "/static_pages/home"

Try running rake routes for more information on available routes. 
Run Code Online (Sandbox Code Playgroud)

耙路线告诉我这个:

root  /                  static_pages#home
help  /help(.:format)    static_pages#help
about  /about(.:format)   static_pages#about
contact  /contact(.:format) static_pages#contact
Run Code Online (Sandbox Code Playgroud)

我的routes.rb文件:

MyApp::Application.routes.draw do
root :to => 'static_pages#home'

match '/help',    :to => 'static_pages#help'
match '/about',   :to => 'static_pages#about'
match '/contact', :to =>'static_pages#contact'


end
Run Code Online (Sandbox Code Playgroud)

有人有个主意吗?

ruby ruby-on-rails ruby-on-rails-3

4
推荐指数
2
解决办法
1万
查看次数

打印变量

我的问题可能很简单,但我无法在任何地方找到答案.

创建类时,例如:

class Book
  @author = "blabla"
  @title = "blabla"
  @number_of_pages"
Run Code Online (Sandbox Code Playgroud)

我想创建一个方法来打印我的变量.我尝试时遇到问题:

def Print
  puts @author, @title, @number_of_pages
end
Run Code Online (Sandbox Code Playgroud)

我一无所获.

当我尝试:

def Print
  puts "@author, @title, @number_of_pages"
end
Run Code Online (Sandbox Code Playgroud)

我直截了当:"@ author,@ title,@ number_of_pages"

如何使Print方法打印出变量的值?

ruby oop

4
推荐指数
1
解决办法
317
查看次数

如何将随机数写入文件?

我需要创建一个包含 20 个随机数的文件。我试过这个:

 for i in {1..20}; do n=$RANDOM; echo "no $i    $n">random number;done
Run Code Online (Sandbox Code Playgroud)

影响:

生成了数字,但它们不在文件中。我的错误在哪里?

linux random

2
推荐指数
1
解决办法
1806
查看次数

测试失败Michael Hartl Ruby on Rails Turorial Chapter 10.28

我和Michael Hartl的教程一起工作.目前我在第10章.在第10.28部分,当我尝试运行测试时:

bundle exec rspec spec/requests/authentication_pages_spec.rb
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Failures:

  2) Authentication authorization for non-signed-in users in the Microposts
     controller submitting to the destroy action Failure/Error: before { delete   
     microposts_path (FactoryGirl.create(:micropost)) } ActionController::RoutingError:
     No route matches [DELETE] "/microposts.1" 
     # ./spec/requests/authentication_pages_spec.rb:115:in`block(6 levels) in
     <top required)>'
Run Code Online (Sandbox Code Playgroud)

这是我的authentication_pages_spec.rb:

require 'spec_helper'

describe "Authentication" do
  let(:user) { FactoryGirl.create(:user) } 
    subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1', text: 'Sign in') }
    it { should have_selector('title', text: …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails railstutorial.org

1
推荐指数
1
解决办法
1053
查看次数

Ruby字符串到字符串数组

我有一个像这样的字符串:

"John Godson Michael Meyer Ludovicio Sforza Cezar Borgia Alessandro Batista"
Run Code Online (Sandbox Code Playgroud)

如何将此字符串转换为字符串数组

# ["John", "Godson", "Michael Meyer" (...)]
Run Code Online (Sandbox Code Playgroud)

ruby arrays string

0
推荐指数
1
解决办法
1643
查看次数