2012-02-07 27 views
-2

を保存しない、空白の表示しますモデルを作り直して何かを投稿した後、投稿は空白になり、データはまったく保存されません。Railsのポストはだから私は、私の中でバリデーションを削除</p> </blockquote> <p>「タイトルは空白にすることはできません」私は新しいポストを提出しようとしましたが、私はエラー</p> <blockquote> <p>を取得

私は何をすべきかわからない、私はこの1つに手伝った!

更新!私のモデルで

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> 
ここ

は、コントローラフォームここ

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

されていますお使いのモデルでは、この行では0

+0

フォームがモデルに接続されていないか、属性を誤って割り当てているようです。ビュー(フォーム)とコントローラーのアクションコード – alony

+0

'form_for'を含む対応するビューを表示してください。 – iblue

+0

FirebugまたはChromeデベロッパーツールを使用して、サーバーに送信される投稿データを確認します。 'PostsController#create'アクションでブレークポイントを設定することで' params'ハッシュをデバッグすることもできます。 – iblue

答えて

0

attr_accessible :photo 

は、あなただけの写真が大量に割り当て可能な属性にします。タイトルを含む他のすべての属性は、新しい投稿を作成すると削除されます。

はこれを試してみてください:

attr_accessible :photo, :title 

今では他の属性のタイトルを受け入れますが、しません。

編集:上記の独自のコメントは表示されませんでしたが、既に理解しています。

関連する問題

 関連する問題