【问题标题】:Rails 4.2: Unknown Attribute or Server Error in LogRails 4.2:日志中的未知属性或服务器错误
【发布时间】:2015-08-16 23:50:48
【问题描述】:

我有一个带有select_tagoptions_from_collection_for_select 的表单,我似乎无法通过。在视图中,当上传 id 设置为 uploadzip_id 时,我得到一个 302 重定向,当它设置为 uploadzip_ids 时,我得到一个 Unknown Attribute 错误。

我有点困惑,因为我的关系与外键一起设置。我确实有另一个带有复选框的模型,称为Uploadpdf,它工作正常。

这里是设置..

class Campaign < ActiveRecord::Base
    has_one :uploadzip
end

class Uploadzip < ActiveRecord::Base
    belongs_to :campaign
end

db/schema.rb

  create_table "campaigns", force: :cascade do |t|
    t.string   "name"
    t.text     "comment"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false

  create_table "uploadzips", force: :cascade do |t|
    t.string   "file_name"
    t.string   "file_type"
    t.datetime "date"
    t.integer  "size"
    t.integer  "pages"
    t.string   "file_ident"
    t.string   "md5"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "campaign_id"
  end

  add_foreign_key "uploadzips", "campaigns"

app/controllers/campaign_controller.rb

class CampaignsController < ApplicationController
      def index
        @campaigns = Campaign.all.order("created_at DESC")
      end

      def new
        @campaign = Campaign.new
      end

      def create
        @campaign = Campaign.new(campaign_params)

        if @campaign.save
            flash[:success] = "Campaign Successfully Launched!"
            redirect_to @campaign
        else
            flash[:error] = "There was a problem launching your Campaign."
            redirect_to new_campaign_path
        end
      end

    .....

  private

      def campaign_params
        params.require(:campaign).permit(:name, :comment, :uploadzip_ids, uploadpdf_ids: [])
      end
end

观看次数/广告系列/_form.rb

<%= form_for @campaign, url: {action: "create"} do |f| %>

    .....some typical fields..

    <%= f.label :data_file, class: "right-label" %>

    <%= select_tag campaign[uploadzip_ids], 
                options_from_collection_for_select(
                Uploadzip.all, :id, :file_name
                ), { include_blank: "Include a Zip File" } %>

    .....some more typical fields

<% end %>

更新

我已更改代码以更好地反映建议的外键。创建活动现已成功,但与所选的uploadzip Zip 文件没有关联。调用@campaign.uploadzip 时,返回nil

这是更新后的代码:

<%= select_tag "uploadzip[campaign_id]",
    options_from_collection_for_select(
    Uploadzip.all, :id, :file_name
    ), { include_blank: "Include a Zip File" } %>

我还把控制器params.require改成了..

  def campaign_params
    params.require(:campaign).permit(:name, :comment, :campaign_id, uploadpdf_ids: [])
  end

【问题讨论】:

  • Uploadzip 模型是否应该继承自 ActiveRecord::Base
  • @Daiku - 它在源代码中。不太清楚在我的复制/粘贴中是如何删除的。

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


【解决方案1】:

FaceBook 小组的成员通过添加一点帮助我解决了这个问题 控制器中的额外逻辑..

  if @campaign.save

    zip = Uploadzip.find(params[:uploadzip_id])
    zip.campaign = @campaign
    zip.save

    flash[:success] = "Campaign Successfully Launched!"
    redirect_to @campaign
  else
    flash[:error] = "There was a problem launching your Campaign."
    redirect_to new_campaign_path
  end

..更改了 select_tag 的名称。

  <%= select_tag :uploadzip_id,
      options_from_collection_for_select(
      Uploadzip.all, :id, :file_name
      ), { include_blank: "Include a Zip File" } %>

【讨论】:

    【解决方案2】:

    302 重定向可能不是一件坏事,因为您正在执行redirect_to new_campaign_path。当您在视图和控制器params.permit 部分中使用uploadzip_id 时,记录是否正确创建?

    【讨论】:

    • 302 并不可怕,只是它没有保存记录。我一直在尝试params.permit 部分中的组合,但我不太确定语法应该是什么样的“正确”方式。
    【解决方案3】:

    根据您的关联设置,foreign_key 应该是 campaign_id 而不是 uploadzip_id。您应该根据您的用例更改您的 associationsforeign_key

    我还建议您关注这些Guides 以了解有关关联的更多信息。

    【讨论】:

    • 谢谢帕万。我仍然无法确定该关联。我更新了上面的代码。请让我知道您的想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    相关资源
    最近更新 更多