Rails 帖子显示为空白,未保存

发布于 2025-01-03 07:47:20 字数 5717 浏览 4 评论 0原文

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

“标题不能为空”

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

我不知道该怎么办,我一直坚持这个,求助!

更新!

形式

<% @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>

I tried to submit a new post and I get the error

"Title can't be blank"

So I removed the validations in my model and after trying again and posting something, the post is just blank, no data is saved whatsoever.

I don't know what to do, I stuck on this one, help!

Update!

Here is the form

<% @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 %>

here is the controller:

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

in my model:

    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

and in 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2025-01-10 07:47:20

通过模型中的这一行:

  attr_accessible :photo

您仅可以批量分配照片属性。当您创建新帖子时,所有其他属性(包括标题)都会被删除。

试试这个:

attr_accessible :photo, :title

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

编辑:上面没有看到您自己的评论,但您已经弄清楚了。

With this line in your model:

  attr_accessible :photo

You make only the photo attribute mass-assignable. All other attributes, including the title, are dropped when you create a new post.

Try this:

attr_accessible :photo, :title

It will now accept the title, but not the other attributes.

edit: didn't see your own comment above, but you figured it out already.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文