Rails帖子显示空白,而不是保存

时间:2012-02-07 07:15:44

标签: ruby-on-rails validation posts

我尝试提交新帖子,但我收到了错误

  

"标题不能为空白"

所以我删除了模型中的验证,再次尝试并发布内容后,帖子只是空白,没有任何数据保存。

我不知道该怎么做,我坚持这个,帮助!

更新!

这是表格

<% @post.tags.build %>
<%= form_for @post, :html => {:multipart => true } do |post_form| %>
 <% if @post.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:      </h2>

  <ul>
  <% @post.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
 </div>
 <% end %>


<div class="field">
<%= post_form.file_field :photo %>
</div>

  <div class="field">
   <%= post_form.label :title %><br />
   <%= post_form.text_field :title %>
 </div>
 <div class="field">
<%= post_form.label :url %><br />
<%= post_form.text_field :url %>
</div>
 <div class="field">
   <%= post_form.label :company %><br />
   <%= post_form.text_field :company %>
</div>
<div class="field">
  <%= post_form.label :language %><br />
  <%= post_form.text_field :language %>
</div>
 <div class="field">
  <%= post_form.label :framework %><br />
  <%= post_form.text_field :framework %>
 </div>
  <div class="field">
   <%= post_form.label :details %><br />
   <%= post_form.text_area :details %>
 </div>
 <h2>Tags</h2>
<%= render :partial => 'tags/form' ,
            :locals => {:form => post_form } %>
<div class="actions">
    <%= post_form.submit %>
    </div>
 <% end %>

这是控制器:

class PostsController < ApplicationController

  http_basic_authenticate_with :name => "franklinexpress", :password => "osxuser8", :except => [:index, :show, :new, :edit]

#def search
 #     @posts = Post.search(params[:search])
 #   end






# GET /posts
# GET /posts.json
 def index
   @posts = Post.search(params[:search])
   # @posts = Post.all
    # respond_to do |format|
     #format.html # index.html.erb
     #format.json { render json: @posts }
   #end
 end

 # GET /posts/1
 # GET /posts/1.json
 def show
   @post = Post.find(params[:id])

   respond_to do |format|
     format.html # show.html.erb
     format.json { render json: @post }
   end
 end

 # GET /posts/new
 # GET /posts/new.json
 def new
   @post = Post.new

   respond_to do |format|
     format.html # new.html.erb
     format.json { render json: @post }
   end
 end

   # GET /posts/1/edit
   def edit
   @post = Post.find(params[:id])
 end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

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

  # PUT /posts/1
  # PUT /posts/1.json
 def update
   @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
       format.html { redirect_to @post, notice: 'Post was successfully updated.' }
      format.json { head :ok }
      else
        format.html { render action: "edit" }
       format.json { render json: @post.errors, status: :unprocessable_entity }
     end
   end
 end

   # DELETE /posts/1
   # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
       format.json { head :ok }
    end
  end
end

在我的模型中:

    class Post < ActiveRecord::Base
 validates :title, :presence => true
 validates :url, :presence => true
 validates :company, :presence => true
 validates :language, :presence => true
 validates_attachment_size :photo, :less_than => 4.megabytes
 validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']

 has_many :comments, :dependent => :destroy
   has_many :tags

  attr_accessor :photo_file_name
  attr_accessor :photo_content_type
  attr_accessor :photo_file_size
  attr_accessor :photo_updated_at
  attr_accessible :photo

  accepts_nested_attributes_for :tags, :allow_destroy => :true,
   :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
   #paperclip-------------------------------
   has_attached_file :photo,
                 :url => "/assests/images/:id/:style/:basename.:extension",
                 :path =>   ":rails_root/public/assets/images/:id/:style/:basename.:extension"


   #:style => {:small => "150x200>"}

   def self.search(search)
    if search
     where('title LIKE ?', "%#{search}%")
    # find(:all, :conditions => ['title LIKE ?', "%#{search}%"])
    else
      all
    end
  end    

 end

并在new.html.erb中:

<div id="header-wrap">
<%= image_tag("walLogotiny.png") %>
<div id="searchbartop">
            <%= form_tag posts_path, :method => :get do%>
            <%= text_field_tag :search, params[:search] ,"size" => 100 %>
            <%= submit_tag "Search", :name => nil %>
            <% end %>
</div>

</div>
<div id="container">
<h2>New Site Submission</h2>
<%= render 'form' %>

<%= link_to 'Back', posts_path %>
</div>

1 个答案:

答案 0 :(得分:0)

在您的模型中使用此行:

  attr_accessible :photo

您只能将照片属性设为群发分配。创建新帖子时,将删除所有其他属性,包括标题。

试试这个:

attr_accessible :photo, :title

现在它将接受标题,但不接受其他属性。

编辑:上面没有看到你自己的评论,但你已经弄明白了。