views:

644

answers:

2

I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model.

Associations:

class Link < ActiveRecord::Base
  has_many :tags, :through => :link_tags
  has_many :link_tags

  accepts_nested_attributes_for :tags, :allow_destroy => :false, 
  :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end

class Tag < ActiveRecord::Base
  has_many :links, :through => :link_tags
  has_many :link_tags
end

class LinkTag < ActiveRecord::Base
  belongs_to :link
  belongs_to :tag
end

links_controller Actions:

  def new
    @link = @current_user.links.build
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @link }
    end
  end

  def create
    @link = @current_user.links.build(params[:link])

    respond_to do |format|
      if @link.save
        flash[:notice] = 'Link was successfully created.'
        format.html { redirect_to links_path }
        format.xml  { render :xml => @link, :status => :created, :location => @link }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @link.errors, :status => :unprocessable_entity }
      end
    end
  end

View code from new.html.erb:

<% form_for [current_user, @link], :url => account_links_path do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
<% end %>

And the corresponding partial:

  <%= f.error_messages %>

  <p>
    <%= f.label :uri %><br />
    <%= f.text_field :uri %>
  </p>
  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>

  <h2>Tags</h2>
  <% f.fields_for :tags_attributes do |tag_form| %>
  <p>
    <%= tag_form.label :name, 'Tag:' %>
    <%= tag_form.text_field :name %>
  </p>
  <% unless tag_form.object.nil? || tag_form.object.new_record? %>
  <p>
    <%= tag_form.label :_delete, 'Remove:' %>
    <%= tag_form.check_box :_delete %>
  </p>
  <% end %>
  <% end %>

  <p>
    <%= f.submit 'Update' %>
  </p>

The following line of code, in the create action in the Link controller throws an error:

@link = @current_user.links.build(params[:link])

The error: Tag(#-621698598) expected, got Array(#-609734898)

Are there additional steps needed in the has_many => :through case? These seem to be the only indicated changes for the basic has_many case.

A: 

Try this:

<% f.fields_for :tags_attributes do |tag_form| %>
obiwanchinobi
Different error now: 'undefined method `with_indifferent_access' for "Coding":String'Parameters:{"commit"=>"Update","authenticity_token"=>"fwCGGgcTKOSfxpFJMXmq7IUfRtfOvdOKl31Xys4TKC8=", "link"=>{"tags_attributes"=>{"name"=>"Coding"}, "uri"=>"http://stackoverflow.com", "title"=>"Stack Overflow"}}Thanks for the suggestion. Is there a solution to this? Is it trying to treat the value of one of the attributes as a hash?
Andrew C
Is there a way to get proper formatting for that mess?
Andrew C
Can you please paste your whole form
obiwanchinobi
I've added the form to the original post. Thanks for your help.
Andrew C
A: 

In your controller in the new action (that loads the form partial), are you building a @tag through your link?

So you should see something along the lines of:

@link = Link.new
@tag = @link.tags.build

It might be best to post the contents of the new and create action of your links_controller.

Steve
The controller actions are now in the original post.I'm not creating the tag in the new action. I was following along with the multi-model section here: http://guides.rubyonrails.org/getting_started.html , which didn't indicate controller changes would be necessary. That said, the new form does automatically generate a tag box, despite the absence of any tags.
Andrew C