2012-02-07 3 views
-2

내가 새 게시물을 제출하려고 절약, 빈 표시 레일과 나는 오류를 얻을게시물이 없습니다

그래서 내가의 검증을 제거 "제목은 비워 둘 수 없습니다"내 모델을 만들고 다시 시도하고 무언가를 게시 한 후에는 게시물이 비어 있으며 데이터가 저장되지 않습니다.

나는 무엇을 해야할지 모르겠다. 나는이 하나에 집착했다.

업데이트! 내 모델

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 

지금 제목을 받아 들일 것입니다,하지만 다른 속성.

편집 : 위의 자신 만의 설명을 보지 못했지만 이미 알아 냈습니다.

관련 문제