2016-11-08 25 views
0

編輯:主要問題是,當我添加引用字段時,我做了劇本:引用而不是劇本:引用,所以字段沒有標記爲外鍵。一旦我解開了這些遷移並正確重新配置,我就能夠完成這項工作。如何在Rails中手動保存引用對象

在我的演出時間控制器中,我試圖自動將劇場編號設置爲任何影院擁有用戶輸入的屏幕,但是當我嘗試將其保存爲整數或字符串時,出現錯誤。但是,當我試圖將它作爲劇場對象保存時,我從控制檯中收到「未經許可的參數:影院」,並從導軌應用程序中收到「影院必須存在」錯誤。

showtimes_controller:

class ShowtimesController < ApplicationController 
    before_action :set_theater, only: [:create, :edit] 
    before_action :set_showtime, only: [:show, :edit, :update, :destroy] 

    # GET /showtimes 
    # GET /showtimes.json 
    def index 
    @showtimes = Showtime.all 
    end 

    # GET /showtimes/1 
    # GET /showtimes/1.json 
    def show 
    end 

    # GET /showtimes/new 
    def new 
    @showtime = Showtime.new 
    end 

    # GET /showtimes/1/edit 
    def edit 
    end 

    # POST /showtimes 
    # POST /showtimes.json 
    def create 

    @showtime = Showtime.new(showtime_params) 

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

    # PATCH/PUT /showtimes/1 
    # PATCH/PUT /showtimes/1.json 
    def update 
    respond_to do |format| 
     if @showtime.update(showtime_params) 
     format.html { redirect_to @showtime, notice: 'Showtime was successfully updated.' } 
     format.json { render :show, status: :ok, location: @showtime } 
     else 
     format.html { render :edit } 
     format.json { render json: @showtime.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /showtimes/1 
    # DELETE /showtimes/1.json 
    def destroy 
    @showtime.destroy 
    respond_to do |format| 
     format.html { redirect_to showtimes_url, notice: 'Showtime was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_showtime 
     @showtime = Showtime.find(params[:id]) 
    end 

    def set_theater 
     screenInfo = Screen.where("id = ?", params[:showtime][:screen]) 
     params['showtime']['theater'] = Theater.find(screenInfo[0]['theater_id']) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def showtime_params 
     params.require(:showtime).permit(:date, :time, :archived, :movie_id, :theater, :screen) 
    end 
end 

場次型號:

class Showtime < ApplicationRecord 
    belongs_to :movie 
    belongs_to :theater 
end 

場次_form試圖保存爲整數時

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

     <ul> 
     <% showtime.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :date %> 
    <%= f.date_select :date %> 
    </div> 

    <div class="field"> 
    <%= f.label :time %> 
    <%= f.time_select :time %> 
    </div> 

    <div class="field"> 
    <%= f.label :archived %> 
    <%= f.check_box :archived %> 
    </div> 

    <div class="field"> 
    <%= f.label :movie_id %> 
    <%= f.text_field :movie_id %> 
    </div> 

    <div class="field"> 
    <%= f.label :screen %> 
    <%= f.text_field :screen %> 
    </div> 

    <%= f.hidden_field :theater, :value => "" %> 

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

錯誤:

Theater(#70015922237640) expected, got Fixnum(#11723820) 
試圖保存爲字符串時

錯誤:試圖保存爲劇院對象時

Theater(#70015868755420) expected, got String(#11739240) 

日誌:

Started POST "/showtimes" for IP at 2016-11-08 20:22:37 +0000 
Processing by ShowtimesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"nENPV5d6YRXdcx3H+Xa9ZypGtyFlaTg+zyENGB10TmW9OyWxLR9Dsl7nDoG9irq+3qApiNA2/oEqL5RZ0SXorA==", "showtime"=>{"date(1i)"=>"2016", "date(2i)"=>"11", "date(3i)"=>"8", "time(1i)"=>"2016", "time(2i)"=>"11", "time(3i)"=>"8", "time(4i)"=>"20", "time(5i)"=>"22", "archived"=>"0", "movie_id"=>"2", "screen"=>"1", "theater"=>""}, "commit"=>"Create Showtime"} 
    [1m[36mScreen Load (0.3ms)[0m [1m[34mSELECT "screens".* FROM "screens" WHERE (id = '1')[0m 
    [1m[36mTheater Load (0.2ms)[0m [1m[34mSELECT "theaters".* FROM "theaters" WHERE "theaters"."id" = ? LIMIT ?[0m [["id", 1], ["LIMIT", 1]] 
Unpermitted parameter: theater 
    [1m[35m (0.1ms)[0m [1m[36mbegin transaction[0m 
    [1m[36mMovie Load (0.2ms)[0m [1m[34mSELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT ?[0m [["id", 2], ["LIMIT", 1]] 
    [1m[35m (0.2ms)[0m [1m[31mrollback transaction[0m 
    Rendering showtimes/new.html.erb within layouts/application 
    Rendered showtimes/_form.html.erb (13.6ms) 
    Rendered showtimes/new.html.erb within layouts/application (16.4ms) 
Completed 200 OK in 323ms (Views: 86.5ms | ActiveRecord: 3.9ms) 

如何地獄我保存這個參數?

+1

的正確方法? – inye

+0

'未經許可的參數:影院'這是一個重要的事情,需要注意並考慮到......這意味着你的'permit/require'行缺少一些東西 –

回答

0

您是否嘗試將對象分配給實例變量,並在保存之前進行賦值?

在您before_action

def set_theater 
    @theather = ... # Code to find the theather 
end 

在您創建行動

def create 
    @showtime = Showtime.new(showtime_params) 
    @showtime.theather = @theather 
    ... # Code to save and handle errors 
end 
+0

我得到一個'ActiveModel :: MissingAttributeError(不能寫未知屬性'theater_id'):'使用此錯誤。不知道爲什麼,因爲我服務的整個對象 – vman411gamer

+0

你是否使用任何調試寶石像byebug?看看'set_theather'方法是否正常工作會很酷。 – rebagliatte

+1

「ActiveModel :: MissingAttributeError」這很有趣......你能給我們提供更多關於這個錯誤的上下文嗎?看看你的控制檯窗口或日誌文件(log/development.log)並找到那個錯誤....在它之後應該有一個堆棧跟蹤(這是大約30小時的文件名行後跟數字)。將其複製並粘貼到原始問題中。這個堆棧跟蹤會告訴我們哪一行代碼導致了這個錯誤 - 這對我們確定究竟是哪裏出錯是非常有幫助的。 –

0

你在你的代碼中使用theater代替theater_id在幾個地方,和你需要去改變它在所有的地方,爲了這個工作。

首先 - 你不能選擇我們的形式... HTML不識別的類型theater一個theater並不會通過一個過 - 所以你的形式,需要通過theater_id,而不是(這將是一個整數它可以愉快地處理)。

# eg here make sure it's a theater_id 
<%= f.hidden_field :theater_id, :value => @theater.id %> 

下一個 - 你需要/許可證可能是發生了什麼拋出一些錯誤 - 你需要被theater_id還有:

def showtime_params 
    params.require(:showtime).permit(:date, :time, :archived, :movie_id, :theater_id, :screen) 
end 

現在你需要出去取影院,使用屏幕信息參數:param - 但也請記住,這可能是零一些時間來實現(所以保護子句總是好的):

def set_theater 
    if params[:showtime].present? && params[:showtime][:screen_id].present? 
    screen_info = Screen.find(params[:showtime][:screen_id]) 
    @theater = Theater.find(screenInfo.theater_id) 
    end 
end 

注:我已經更新命名的方案是鐵路標準和除去日荷蘭國際集團在您嘗試將其設置爲低於PARAMS劇院:

params['showtime']['theater'] = Theater.find(screenInfo[0]['theater_id']) 

我不知道你實際上試圖用這行代碼做的,但不管是什麼,則params不會按照這種方式 - 考慮params是「從用戶那裏傳給我們的東西的集合,然後被扔掉」 - 我們不使用它來存儲我們創建控制器的新值。這就是@variable s爲用於

你能解釋更多你想要做什麼,爲什麼你有一個劇院,而不是theater_id我們會想出解決問題的吧:)

+1

感謝您的幫助,但我明白了。必須撤消我在劇場中的遷移:參考而不是劇場:參考,所以它沒有標記爲foreign_key,這使得整個事情變得更加困難 – vman411gamer

相關問題