开发者

One-to-one Relationship Question - Rails

I have a one-to-one relationship between a User and a Partner. In my User model, I have the following:

 has_one :partner, :class_name => "Partner"

I want to create a new Partner if "relation_status == coupled" and then make the user go to a new form to fill out information on his/her Partner. So in my UsersController, I have

respond_to do |format|
    if @user.save && @user.relation_status != "Coupled"
           format .html { redirect_to @user, notice: "User was successfully created ID   
      was #{@id} "}
           format.json { render json: @user, status: :created, location: @user }

         elsif @user.save && @user.relation_status == "Coupl开发者_运维问答ed"
           format .html { redirect_to partner_signup_path, notice: "Time for your patna"     
            }
           format.json { render json: @user, status: :created, location: @user }
    else
           format.html { render action: "new" }
           format.json { render json: @user.errors, status: :unprocessable_entity }
     end
   end
 end

In my PartnersController, I have the basic new and create actions that successfully create a new Partner. Obviously, there's no way to have the Partner's ID assigned to the User's foreign key - partner_id automatically by doing what I did above. How can I go about doing this correctly? Should I be doing this in the User model instead? I'm kind of a Rails newbie. Thanks for the help.


The answer is to keep the user in session in the UsersController -> session[:id] = @user.id . Then in the PartnersController, I did: @partner = Partner.new(params[:user]) @partner.save @user = User.find(session[:id]) @user.partner = @partner

Voila.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜