Rails 4.2: Unknown Attribute or Server Error in Log

我的梦境 提交于 2019-12-10 10:58:13

问题


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.

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.

Here is the set up..

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 %>

Update

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.

Here is the updated code:

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

I also changed the controller params.require to..

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

回答1:


As per your association set-up,the foreign_key should be campaign_id not uploadzip_id. You should either change your associations or foreign_key according to your use-case.

And also I recommend you to follow these Guides to know more about associations.




回答2:


The 302 redirect may not be a bad thing, since you're doing a redirect_to new_campaign_path. Are the records created correctly when you use uploadzip_id in both the view and controller params.permit section?




回答3:


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

..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" } %>


来源:https://stackoverflow.com/questions/30608753/rails-4-2-unknown-attribute-or-server-error-in-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!