Rails 4.2:日志中的未知属性或服务器错误 [英] Rails 4.2: Unknown Attribute or Server Error in Log

查看:43
本文介绍了Rails 4.2:日志中的未知属性或服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a form with a select_tag and options_from_collection_for_select that I can't seem to get to pass. In the view, when the upload id is set to uploadzip_id I get a 302 redirect and when it's set to uploadzip_ids, I get a Unknown Attribute error.

我有点困惑,因为我的关系与外键一起设置.我确实有另一个模型,带有名为 Uploadpdf 的复选框,效果很好.

I'm a bit confused as I have my relationship set up along with the foreign key. I do have another model with checkboxes called Uploadpdf that works fine.

这是设置..

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

views/campaigns/_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.

I have changed the code to better reflect the foreign key as suggested. Creating a campaign is now successful but it's not associating with the chosen uploadzip Zip file selected. When calling @campaign.uploadzip, it returns nil.

这是更新的代码:

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

我还将控制器 params.require 更改为..

I also changed the controller params.require to..

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

推荐答案

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

Member of a FaceBook group helped me figure it out by adding a little extra logic in the controller..

  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 的名称.

..which was met with changing the select_tag's name.

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

这篇关于Rails 4.2:日志中的未知属性或服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆