如何更正main_image的未定义方法?

时间:2017-06-10 02:29:39

标签: ruby-on-rails ruby

所以我试图在portfolio_controller.rb中构建Rails中的show功能:

class PortfoliosController < ApplicationController
    def index
        # this calls the model
        @portfolio_items = Portfolio.all
    end

    def new
        @portfolio_item = Portfolio.new
    end

    def create
        @portfolio_item = Portfolio.new(params.require(:portfolio).permit(:title, :subtitle, :body))

        respond_to do |format|
            if @portfolio_item.save
                format.html {redirect_to portfolios_path, notice: 'Your portfolio item is now live'}
            else
                format.html {render :new}
            end
        end
    end

    def edit
        # its looking in the params in URI
        @portfolio_item = Portfolio.find(params[:id])
    end

    def update
        @portfolio_item = Portfolio.find(params[:id])

        respond_to do |format|
      if @portfolio_item.update(params.require(:portfolio).permit(:title, :subtitle, :body))
        format.html { redirect_to portfolios_path, notice: 'The record was successfully updated.' }
      else
        format.html { render :edit }
      end
    end

    def show
        @portfolio_item = Portfolio.find(params[:id])   
    end
    end
end

我在上面的控制器中创建了show方法,它需要能够找到它需要使用的项目组合项目并将其传递给show文件并将其呈现给链接。

我没有把show方法留空,因为我确实需要知道我在说什么组合。我知道你看到很多DRY,但最终我会把它全部放在before_action中。

我在views文件夹中创建了一个show.html.erb:

<%= image_tag @portfolio_item.main_image %>
<h1><%= @portfolio_item.title %></h1>
<em><%= @portfolio_item.subtitle %></em>
<p><%= @portfolio_item.body %></p>

但我收到了这个错误:

enter image description here

在展示页面中,我这样做:

<%= @portfolio_item.inspect %>

我在浏览器中没有,但在我的rails控制台中我有大量数据:

2.3.3 :002 > Portfolio.all
  Portfolio Load (0.3ms)  SELECT  "portfolios".* FROM "portfolios" LIMIT $1  [["LIMIT", 11]]
 => #<ActiveRecord::Relation [#<Portfolio id: 2, title: "Portfolio title: 1", subtitle: "My great service", body: "Gluten-free pug cloud bread raclette. \n\t\tSucculent...", main_image: "http://via.placeholder.com/600x400", thumb_image: "http://via.placeholder.com/350x200", created_at: "2017-06-06 02:14:05", updated_at: "2017-06-06 02:14:05">, #<Portfolio id: 3, title: "Portfolio title: 2", subtitle: "My great service", body: "Gluten-free pug cloud bread raclette. \n\t\tSucculent...", main_image: "http://via.placeholder.com/600x400", thumb_image: "http://via.placeholder.com/350x200", created_at: "2017-06-06 02:14:05", updated_at: "2017-06-06 02:14:05">, #<Portfolio id: 4, title: "Portfolio title: 3", subtitle: "My great service", body: "Gluten-free pug cloud bread raclette. \n\t\tSucculent...", main_image: "http://via.placeholder.com/600x400", thumb_image: "http://via.placeholder.com/350x200", created_at: "2017-06-06 02:14:05", updated_at: "2017-06-06 02:14:05">, #<Portfolio id: 6, title: "Portfolio title: 5", subtitle: "My great service", body: "Gluten-free pug cloud bread raclette. \n\t\tSucculent...", main_image: "http://via.placeholder.com/600x400", thumb_image: "http://via.placeholder.com/350x200", created_at: "2017-06-06 02:14:05", updated_at: "2017-06-06 02:14:05">, #<Portfolio id: 7, title: "Portfolio title: 6", subtitle: "My great service", body: "Gluten-free pug cloud bread raclette. \n\t\tSucculent...", main_image: "http://via.placeholder.com/600x400", thumb_image: "http://via.placeholder.com/350x200", created_at: "2017-06-06 02:14:05", updated_at: "2017-06-06 02:14:05">, #<Portfolio id: 8, title: "Portfolio title: 7", subtitle: "My great service", body: "Gluten-free pug cloud bread raclette. \n\t\tSucculent...", main_image: "http://via.placeholder.com/600x400", thumb_image: "http://via.placeholder.com/350x200", created_at: "2017-06-06 02:14:05", updated_at: "2017-06-06 02:14:05">, #<Portfolio id: 9, title: "Portfolio title: 8", subtitle: "My great service", body: "Gluten-free pug cloud bread raclette. \n\t\tSucculent...", main_image: "http://via.placeholder.com/600x400", thumb_image: "http://via.placeholder.com/350x200", created_at: "2017-06-06 02:14:05", updated_at: "2017-06-06 02:14:05">, #<Portfolio id: 5, title: "Portfolio title: 5", subtitle: "My great service edited", body: "Gluten-free pug cloud bread raclette. \r\n\t\tSucculen...", main_image: "http://via.placeholder.com/600x400", thumb_image: "http://via.placeholder.com/350x200", created_at: "2017-06-06 02:14:05", updated_at: "2017-06-06 18:01:58">, #<Portfolio id: 1, title: "Portfolio title: 0", subtitle: "My great service", body: "Our seeds file uses the block system. The id start...", main_image: "http://via.placeholder.com/600x400", thumb_image: "http://via.placeholder.com/350x200", created_at: "2017-06-06 02:14:05", updated_at: "2017-06-06 18:15:31">]>

如果查看我的schema.rb文件:

ActiveRecord::Schema.define(version: 20170605121642) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "blogs", force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "portfolios", force: :cascade do |t|
    t.string "title"
    t.string "subtitle"
    t.text "body"
    t.text "main_image"
    t.text "thumb_image"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "skills", force: :cascade do |t|
    t.string "title"
    t.integer "percent_utilized"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

您看到main_image位于portfolio表中。所以我不清楚为什么我会收到这个错误。

这是portfolio / index.html.erb的代码:

<h1>Portfolio Items</h1>

<%= link_to "Create New Item", new_portfolio_path %>

<% @portfolio_items.each do |portfolio_item| %>
    <p><%= link_to portfolio_item.title, portfolio_path(portfolio_item) %></p>
    <p><%= portfolio_item.subtitle %></p>
    <p><%= portfolio_item.body %></p>
    <!-- image_tag is a method to render an image -->
    <!-- Run this code unless portfolio_item.thumb_image is nil -->
    <%= image_tag portfolio_item.thumb_image unless portfolio_item.thumb_image.nil? %>
    <%= link_to "Edit", edit_portfolio_path(portfolio_item) %>
<% end %>

这是我的佣金路线| grep投资组合:

danales-MacBook-Pro:dancortesPortfolio danale$ rake routes | grep portfolios
    portfolios GET    /portfolios(.:format)          portfolios#index
               POST   /portfolios(.:format)          portfolios#create
 new_portfolio GET    /portfolios/new(.:format)      portfolios#new
edit_portfolio GET    /portfolios/:id/edit(.:format) portfolios#edit
     portfolio GET    /portfolios/:id(.:format)      portfolios#show
               PATCH  /portfolios/:id(.:format)      portfolios#update
               PUT    /portfolios/:id(.:format)      portfolios#update
               DELETE /portfolios/:id(.:format)      portfolios#destroy

1 个答案:

答案 0 :(得分:3)

我认为,这是确切的错误..您已在show内写了update行动

def update
  @portfolio_item = Portfolio.find(params[:id])
  respond_to do |format|
    if @portfolio_item.update(params.require(:portfolio).permit(:title, :subtitle, :body))
      format.html { redirect_to portfolios_path, notice: 'The record was successfully updated.' }
    else
      format.html { render :edit }
    end
  end
end
def show
  @portfolio_item = Portfolio.find(params[:id])
end