2012-09-21 72 views
0

我有一個管理與項目關聯的文件的控制器。當我點擊保存表單上我得到如下:Project_ID不會保存

Started POST "/project_files" for 127.0.0.1 at 2012-09-20 17:10:47 -0500 
Processing by ProjectFilesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"v4iELoC9hk0rIgx5YmIXbi1JlLd+etXkNRynad0q7dA=", "project_files"=>{"project_id"=>"1"}, "commit"=>"Create Project file"} 
    (0.1ms) begin transaction 
    SQL (6.3ms) INSERT INTO "project_files" ("created_at", "file_content_type", "file_file_name", "file_file_size", "file_updated_at", "project_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Thu, 20 Sep 2012 22:10:47 UTC +00:00], ["file_content_type", nil], ["file_file_name", nil], ["file_file_size", nil], ["file_updated_at", nil], ["project_id", nil], ["updated_at", Thu, 20 Sep 2012 22:10:47 UTC +00:00]] 
[paperclip] Saving attachments. 
    (6.7ms) commit transaction 
Redirected to http://localhost:3000/project_files/1 
Completed 302 Found in 21ms (ActiveRecord: 13.1ms) 

正如你可以在params哈希表PROJECT_ID看顯然是可用的,但是,你也可以看到它不被保存。

在我schema.rb我有以下幾點:

create_table "project_files", :force => true do |t| 
    t.integer "project_id" 
    t.datetime "created_at",  :null => false 
    t.datetime "updated_at",  :null => false 
    t.string "file_file_name" 
    t.string "file_content_type" 
    t.integer "file_file_size" 
    t.datetime "file_updated_at" 
    end 

我的看法是這樣的:

<%= form_for(@project_file) do |f| %> 
    <% if @project_file.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@project_file.errors.count, "error") %> prohibited this project_file from being saved:</h2> 

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

    <div class="field"> 
    <%= f.label :project_id %><br /> 
    <%= collection_select(:project_files, :project_id, @projects, :id, :title)%> 
    </div> 

    <%= f.file_field :file %> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

我的控制器看起來是這樣的:

# GET /project_files/new 
    # GET /project_files/new.json 
    def new 
    @project_file = ProjectFile.new 
    @projects = Project.all 

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

# POST /project_files 
    # POST /project_files.json 
    def create 
    @project_file = ProjectFile.new(params[:project_file]) 

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

任何想法上我做錯了什麼?

+1

嘗試檢查'PARAMS [:project_files] [:PROJECT_ID]'在你創建的行動,看是否值已設置。 – MurifoX

回答

1

猜你已經有了一個錯字

<%= collection_select(:project_files, :project_id, @projects, :id, :title)%> 

很可能需要:project_file,不:project_files

+0

通過刪除這些錯字相關問題,我們可以使用您的幫助![清除堆棧溢出](http://meta.stackexchange.com/q/167342)。你可以通過在這個問題上進行近距離投票來介入一點嗎? –