Note that there are some explanatory texts on larger screens.

plurals
  1. POBuild Associations Rails 3.1
    text
    copied!<p>Anytime I create a user and log in this exception is thrown on the redirect: <code>Couldn't find User without an ID</code>. </p> <p>I have a <code>has_many</code> association like so:</p> <pre><code>class Post &lt; ActiveRecord::Base has_many :tags belongs_to :user attr_accessible :title, :description, :content, :tags_attributes accepts_nested_attributes_for :tags, allow_destroy: true, reject_if: lambda {|attrs| attrs.all? {|key, value| value.blank?}} end </code></pre> <p>followed by:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :posts attr_accessible :codename, :password, :password_confirmation end </code></pre> <p>There is a third model 'Tags' which has a <code>belongs_to post</code> but that does not appear to be causing the problem. There is also a SessionsController with <code>new</code>,<code>create</code>, and <code>destroy</code> actions (code below). </p> <p>The <code>find_user</code> method is run only on the new and create actions in the PostsController</p> <pre><code>before_filter :find_user def find_user @user = User.find(params[:user_id]) end def new @post = @user.posts.build(tags: Tag.new) respond_to do |format| format.html end end def create @post = @user.posts.build(params[:post]) @tags = @post.tags.build(params[:tags]) respond_to do |format| if @post.save format.html { redirect_to @post, notice: 'Post was successfully created.' } else format.html { render action: :new } end end end </code></pre> <p>and the User.authenticate method / SessionsController <code>create</code> action</p> <pre><code>def self.authenticate(codename, password) user = User.find_by_codename(codename) unless user &amp;&amp; user.authenticate(password) raise "You are not who you say you are." end user end def create session[:user_id] = User.authenticate(params[:codename], params[:password]).id redirect_to action: session[:intended_action], controller: session[:intended_controller], success: "You're In!" end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload