首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >初学者@Rails: nil:NilClass的未定义方法“`title”,无法找到使用‘id’=编辑的决定

初学者@Rails: nil:NilClass的未定义方法“`title”,无法找到使用‘id’=编辑的决定
EN

Stack Overflow用户
提问于 2015-10-20 09:56:40
回答 1查看 223关注 0票数 2

对于您的上下文:这是我第一次尝试创建一个应用程序。我刚刚开始编码:-)。

我正在尝试一个简单的CRUD设置来工作。

现在我有两个问题,我不能把我的头弄清楚:

  1. 我的条目不会出现在我的索引页面上。它给出了如下错误:“未定义的方法”“`title”表示为nil:NilClass。该模型包含以下列: string :title、text :forecast、date :review_date
  2. 如果我转到决策/编辑,它会给我以下错误:“无法找到带有‘id’=编辑的决定

这是我的密码:

主计长:

代码语言:javascript
复制
  class DecisionsController < ApplicationController
before_action :find_decision, only: [:show, :edit, :update]

  def index
# gets all rows from decision table and puts it in @decision variable
    @decisions = Decision.all
  end

  def show
# find only the decision entry that has the id defined in params[:id]
    @decision = Decision.find(params["id"])
  end
# shows the form for creating a entry
  def new
    @decision = Decision.new
  end
# creates the entry
  def create
    @decision = Decision.new(decision_params)
      if @decision.save
        redirect_to @decision
      else
        render 'new'
      end
  end
# shows the form for editing a entry
  def edit
      @decision = Decision.find(params["id"])
  end
# updates the entry
  def update
  end

  def destroy
  end

  private
  def find_decision
      @decision = Decision.find(params["id"])
  end

  def decision_params
    params.require(:decision).permit(:title, :forecast, :review_date)
  end

end

索引视图

代码语言:javascript
复制
<h1>Hello World ^^</h1>

<% @decisions.each do |descision| %>
 <p><%= @decision.title %></p>
<% end %>

routes.rb

代码语言:javascript
复制
Rails.application.routes.draw do
  resources :decisions
  root 'decisions#index'
end

我整个上午都在研究这两个人,但我想不出来。如果你们能帮我看看,我会很有帮助的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-20 12:37:27

我刚开始编码

欢迎!!

我的entries没有出现在我的索引页面上。

我相信你是说decisions,对吧?

如果是这样的话,您必须记住,如果在Ruby中调用一个循环,那么在尝试调用它之前,需要一些条件逻辑来确定它是否实际填充了任何数据:

代码语言:javascript
复制
#app/views/decisions/index.html.erb
<% if @decisions.any? %>
   <% @decisions.each do |decision| %>
      <%= content_tag :p, decision.title %>
   <% end %>
<% end %>

这必须由适当的控制器代码匹配:

代码语言:javascript
复制
#app/controllers/decisions_controller.rb
class DecisionsController < ApplicationController
    before_action :find_decision, only: [:show, :edit, :update, :destroy]

    def index
       @decisions = Decision.all
    end

    def show
    end

    def new
       @decision = Decision.new
    end

    def create
       @decision = Decision.new decision_params
       @decision.save ? redirect_to(@decision) : render('new')
    end

    def edit
    end

    def update
    end

    def destroy
    end

    private
    def find_decision
        @decision = Decision.find params["id"]
    end

    def decision_params
      params.require(:decision).permit(:title, :forecast, :review_date)
    end
end

这将使您能够在视图中调用@decisions@decision,具体取决于您要访问的路由。

重要的一点是当你说..。

decisions/edit给出了以下错误:Couldn't find Decision with 'id'=edit'

..。这个问题是由处理Rails路由的方式引起的:

因为Rails是https://en.wikipedia.org/wiki/Object-oriented_programming,所以每一组路由都对应于对象集合或成员对象。这就是为什么像edit这样的路由需要传递一个"id“--它们被设计用来处理成员对象。

因此,当您访问任何“成员”路由(decisions/:iddecisions/:id/edit)时,您必须提供一个id,以便Rails能够从db中提取适当的记录:

代码语言:javascript
复制
#app/views/decisions/index.html.erb
<% if @decisions.any? %>
   <% @decisions.each do |descision| %>
      <%= link_to "Edit #{decision.title}", decision_edit_path(decision) %>
   <% end %>
<% end %>

我可以解释得更多--以上所述现在应该适用于你。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33233521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档