我的 rails 应用程序抛出未定义的方法“post_post_path”异常。我正在尝试使用 'Paperclip' 将图像附加到我已经存在的模型

My rails app throws undefined method `post_post_path' exception.. I m trying to use 'Paperclip' to attach image to my already existing model

我是 rails 的新手,我正在尝试尽可能多地学习。我目前正在开发一个非常基本的 rails 应用程序,它显示 post 和允许添加 post 和图像,也可以编辑和删除它们,无需 脚手架 。这是我的代码,

控制器:wall_controller.rb

class WallController < ApplicationController
def show_all
  @feeds = Post.all
end
def add
  @feeds = Post.create(params[:feeds])
  @feeds.save
  unless @feeds.valid?
    flash[:error] = @feeds.errors.full_messages.join("<br>").html_safe
  else
    #set flash[:success] to "Feed added successfully"
  end
 redirect_to :action => 'show_all'
 end
end

视图:show_all.html.erb

  <div id = "add_feeds">
 <%= form_for(@feeds,:method => "post",:html => { :multipart => true })  do |f| %>
 <div class="form-group">
 <%= f.text_field :content_text %>
 <%= f.file_field :pic, class: 'form-control'%>
 </div>
 <%= f.submit 'Upload', class: 'btn btn-primary' %>
 <% end %>
</div>
<div id="container">
<ul>
   <% @feeds.each do |feed| %>
   <div id ="posts" class = "well">
    <li id=feed>
    <%= image_tag feed.pic.url %>
      <i>
        <%= feed.content %>
      </i>
      <%= link_to "Edit", "wall/#{feed.id}/edit_feed" %>
      <%= link_to "Delete", "wall/#{feed.id}"%>
    </li>
   </div>
   <% end %>
  <ul>
</div>

型号:post.rb

class Post < ActiveRecord::Base
  attr_accessible :content ,:pic
  has_attached_file :pic, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :pic, content_type: /\Aimage\/.*\Z/
end

架构:

ActiveRecord::Schema.define(:version => 20151006014835) do

  create_table "posts", :force => true do |t|
    t.text     "content"
    t.datetime "created_at",       :null => false
    t.datetime "updated_at",       :null => false
    t.string   "pic_file_name"
    t.string   "pic_content_type"
    t.integer  "pic_file_size"
    t.datetime "pic_updated_at"
  end
 end

routes.rb 文件

  match "home" => "wall#show_all", via: :get
  match "home" => "wall#add", via: :post

我哪里出错了需要帮助..在此先感谢.. `

I am a newbie to rails

双会场!


首先,阅读 CRUD and how it applies to rails...

#app/controllers/walls_controller.rb
class WallsController < ApplicationController
    def index
      @feeds = Post.all
    end
   def new
     @feed = Post.new feed_params
     if @feed.save
       flash[:error] = @feeds.errors.full_messages.join("<br>").html_safe
     else
        render :all, success:"Feed added successfully"
     end
   end

   private

   def feed_params
      params.require(:feed).permit(:content_text, :pic)
   end
end

需要了解Rails应该如何处理控制器内的各种动作。您的 show_alladd 方法完全违反惯例(这就是您遇到问题的原因)。

此外,查看 object orientation in Rails;你真的应该像你的控制器一样称呼你的模型(一开始) - @feeds = Post.all 真是令人困惑。

#config/routes.rb
root "walls#index"
resources :walls #-> note the plurality 

#app/views/walls/index.html.erb
<%= link_to "New", new_feed_path %>
<% @feeds.each do |feed| %>
   <%= image_tag feed.pic.url %>
   <%= feed.content %>
   --
   <%= link_to "Edit", feeds_edit_path(feed) %>
   <%= link_to "Delete", feeds_path(feed), method: :delete %>
<% end %>

#app/views/walls/new.html.erb
<%= form_for @feed do |f| %>
  <%= %= f.text_field :content %>
  <%= f.file_field :pic %>
  <%= f.submit %>
<% end %>