[英]Rails 4 active record model has_many through associations?
I been struggling with this concept for some time. 我一直在为这个概念而苦苦挣扎。 I read the guide and still - I am having a hard time getting this to work as I need it to.
我仍然阅读了该指南-我很难按需使用它。 http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
For me this topic has been difficult to understand because of all the conventions. 对我而言,由于所有约定,这个主题很难理解。 My code below is an attempt.
我下面的代码是尝试。 Please feel free to offer suggestions for best practices besides error corrections if you see fit.
如果您认为合适,请随时提供错误纠正以外的最佳做法建议。 Thanks a million!
太感谢了!
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
What I would like to do is go to url/campaign
to create a new record with a nested form which has owner
and shorturl
params. 我想做的是去
url/campaign
以嵌套形式创建一个新记录,该记录具有owner
和shorturl
参数。
The form that I have looks like this: 我的表格如下所示:
<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 %>
The Routes
are setup as follows: Routes
设置如下:
post '/campaign', to: 'campaign#create'
get '/campaign', to: 'campaign#index'
My Campaign#controller
looks like this: 我的
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
As of now when I submit the form I get the following error: 到目前为止,当我提交表单时,出现以下错误:
ActionController::UnknownFormat in CampaignController#create
ActionController::UnknownFormat
What is wrong with the above? 上面有什么问题? Thanks again!
再次感谢!
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
But I have my params permitted properly (at least I think I do) according to my campaign#controller
但是根据我的
campaign#controller
#controller,我的参数已被适当地允许(至少我认为是这样)
params.require(:campaign).permit(shorturl_attributes: [:redirect], owner_attributes: [:email])
and my models: 和我的模型:
accepts_nested_attributes_for :owner
accepts_nested_attributes_for :shorturl
Why am I getting: Unpermitted parameters: shorturl, owner
? 为什么会得到:
Unpermitted parameters: shorturl, owner
?
ActionController::UnknownFormat
is raised when the action doesn't handle the request format. 当操作不处理请求格式时,引发
ActionController::UnknownFormat
。
Your controller only responds to JSON format, but your POST format is probably application/x-www-form-urlencoded
or multipart/form-data
. 您的控制器仅响应JSON格式,但您的POST格式可能是
application/x-www-form-urlencoded
或multipart/form-data
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.