Rails 4活动记录模型has_many通过关联?

时间:2014-10-18 00:34:12

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

我一直在努力解决这个问题。我阅读了指南并且仍然 - 我很难让它工作,因为我需要它。 http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

对我而言,由于所有惯例,这个主题很难理解。我的下面的代码是一个尝试。如果您认为合适,请随时提供除错误更正之外的最佳实践建议。万分感谢!

class Owner < ActiveRecord::Base
  has_many :shorturls, through: :campaigns
end

class Shorturl < ActiveRecord::Base
  has_many :owners, through: :campaigns
end

class Campaign < ActiveRecord::Base
  belongs_to :owner
  belongs_to :shorturl
  accepts_nested_attributes_for :owner
  accepts_nested_attributes_for :shorturl
end

我想要做的是转到url/campaign创建一个包含ownershorturl参数的嵌套表单的新记录。

我的表格看起来像这样:

<h1>New Campaign</h1>

<%= form_for :campaign do |f| %>

  <%= f.fields_for :shorturl do |shorturl| %>
    <%= shorturl.label :redirect %><br>
    <%= shorturl.text_field :redirect %>
  <% end %>

  <%= f.fields_for :owner do |owner| %>
    <%= owner.label :email %><br>
    <%= owner.text_field :email %>
  <% end %>

  <%= f.submit %>

<% end %>

Routes的设置如下:

post '/campaign', to: 'campaign#create'
get '/campaign', to: 'campaign#index'

我的Campaign#controller看起来像这样:

class CampaignController < ApplicationController
  def index
    @campaign = Campaign.new
  end

  def create
    @campaign = Campaign.new(campaign_params)

    respond_to do |format|
      if @campaign.save
        format.json { render :index, status: :created, location: @campaign }
      else
        format.json { render json: @campaign.errors, status: :unprocessable_entity }
      end
    end
  end

    private
    def set_campaign
      @campaign = campaign.find(params[:id])
    end

    def campaign_params
      params.require(:campaign).permit(shorturl_attributes: [:redirect], owner_attributes: [:email])
    end
end

截至目前,当我提交表单时,我收到以下错误:

ActionController::UnknownFormat in CampaignController#create
ActionController::UnknownFormat

以上是什么问题?再次感谢!

更改respond_to块格式后更新

,我收到unpermitted_pa​​rams错误:请参阅日志:

Started POST "/campaign" for ::1 at 2014-10-17 21:04:08 -0400
Processing by CampaignController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9VOe+9VDhVaXR/eJKxA8PUJdZGoRQQZvWpfdX0SeDZrzL1gTVICMTjQkR+rk43WPSAxLz7s73YwhF1HdV8Qazg==", "campaign"=>{"shorturl"=>{"redirect"=>"http://www.somesite.com"}, "owner"=>{"email"=>"test@example.com"}}, "commit"=>"Save Campaign"}
Unpermitted parameters: shorturl, owner
   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "campaigns" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  [["created_at", "2014-10-18 01:04:08.211040"], ["updated_at", "2014-10-18 01:04:08.211040"]]
   (0.4ms)  COMMIT
  Rendered campaign/index.html.erb within layouts/application (4.5ms)
Completed 200 OK in 112ms (Views: 108.2ms | ActiveRecord: 0.8ms)
::1 - - [17/Oct/2014:21:04:08 -0400] "POST /campaign HTTP/1.1" 200 - 0.1166

但根据我的campaign#controller

,我可以正确地允许我的参数(至少我认为我这样做)
params.require(:campaign).permit(shorturl_attributes: [:redirect], owner_attributes: [:email])

和我的模特:

  accepts_nested_attributes_for :owner
  accepts_nested_attributes_for :shorturl

为什么我会得到:Unpermitted parameters: shorturl, owner

1 个答案:

答案 0 :(得分:1)

当操作未处理请求格式时,将引发

ActionController::UnknownFormat

您的控制器仅响应JSON格式,但您的POST格式可能是application/x-www-form-urlencodedmultipart/form-data