简体   繁体   中英

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

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.

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:

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

My Campaign#controller looks like this:

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!

update after changing the respond_to block format, I am getting an unpermitted_params error: see logs:

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

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 ?

ActionController::UnknownFormat is raised when the action doesn't handle the request format.

Your controller only responds to JSON format, but your POST format is probably application/x-www-form-urlencoded or multipart/form-data .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM