如何在Rails中

时间:2016-11-08 20:26:10

标签: ruby-on-rails activerecord rails-activerecord

编辑:主要问题是,当我添加引用字段时,我执行了剧院:引用而不是剧院:引用因此字段未标记为外键。一旦我解除了这些迁移并正确地重新进行了迁移,我就能够完成这项工作。

在我的showtimes控制器中,我试图自动将影院ID设置为用户输入的屏幕所拥有的任何影院,但是当我尝试将其保存为整数或字符串时,我收到错误。然而,当我尝试将其保存为剧院对象时,我从控制台获得“未经许可的参数:影院”,以及来自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

showtimes模特:

class Showtime < ApplicationRecord
  belongs_to :movie
  belongs_to :theater
end

Showtimes _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)

尝试保存为Theater对象时记录:

    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)

我该怎么保存这个参数?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您在代码中的多个位置使用theater代替theater_id,并且您需要在所有位置更改它,以便使其正常工作。

首先 - 您无法在我们的表单中选择theater ... html无法识别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

现在你需要使用screen-info参数取出影院 - 但是请记住,这可能会在nil中出现一段时间(所以一个guard-clause总是好的):

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是&#34;一组从用户传递给我们的东西,然后扔掉了#34; - 我们不会用它来存储我们在控制器上创建的新值。那是@variable的用途

您能解释一下您正在尝试做的事情吗?我们将找到正确的方法:)