views:

167

answers:

2

Right now I'm building a project management app in rails, here is some background info:

Right now i have 2 models, one is User and the other one is Client. Clients and Users have a one-to-one relationship (client -> has_one and user -> belongs_to which means that the foreign key it's in the users table)

So what I'm trying to do it's once you add a client you can actually add credentials (add an user) to that client, in order to do so all the clients are being displayed with a link next to that client's name meaning that you can actually create credentials for that client.

So in order to do that I'm using a helper the link to helper like this.

<%= link_to "Credentials", 
        {:controller => 'user', :action => 'new', :client_id => client.id} %>

Meaning that he url will be constructed like this:

http://localhost:3000/clients/2/user/new

By creating the user for the client with he ID of 2.

And then capturing the info into the controller like this:

@user = User.new(:client_id => params[:client_id])

EDIT: This is what i currently have in my View/Controller and Routes

I keep getting this error: No route matches "/clients//user" with {:method=>:post}

Routes

ActionController::Routing::Routes.draw do |map|
  map.resources :users
  map.resources :clients, :has_one => :user
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

Controller

class UsersController < ApplicationController
  before_filter :load_client

  def new
    @user = User.new
    @client = Client.new
  end

  def load_client
    @client = Client.find(params[:client_id])
  end

  def create
    @user = User.new(params[:user])
    @user.client_id = @client.id
    if @user.save
      flash[:notice] = "Credentials created"
      render :new
    else
      flash[:error] = "Credentials created failed"
    render :new
   end
  end

View

   <% form_for @user, :url => client_user_url(@client)  do |f| %> 
        <p>
            <%= f.label :login, "Username" %>
            <%= f.text_field :login %>
        </p>
        <p>
            <%= f.label :password, "Password" %>
            <%= f.password_field :password %>
        </p>

        <p>
            <%= f.label :password_confirmation, "Password Confirmation" %>
            <%=  f.password_field :password_confirmation %>
        </p>

        <%= f.submit "Create", :disable_with => 'Please Wait...' %>

    <% end %>
A: 

Your form tag is wrong, you are posting to /users without the :client_id.

Try this:

<% form_for @user, :url => {:controller => 'users', :action => 'new', :client_id => @client.id} do |f| >

Alternatively, you could use nested resources:

config/routes.rb

map.resources :clients do |clients|
  clients.resources :users
end

Controller

class UsersController < ApplicationController
  before_filter :load_client

  def load_client
    @client = Client.find(params[:client_id])
  end

  # Your stuff here
end

View

<% form_for [@client, @user] do |f| %>
elektronaut
I tried the first response, and it's claiming that the @client object is empty.Also tried the second one and it was still a no-go. (I added a @client = Client.new in the new method of the users controllers and still). By the way i also have this in my routes map.resources :clients, :has_one => :userIsn't it the same as what you're stating?
Gotjosh
That's right. You need to load the client somewhere in your controller as well. I've edited the answer and added an example.
elektronaut
I'm sorry if this might sound dumb, but I'm now getting a "undefined method `client_users_path' for #<ActionView::Base:0x10342c738>" Error.
Gotjosh
Oh yeah, :has_one makes it a singleton resource. This should work: <% form_for @user, :url => client_user_path(@client) %>
elektronaut
I keep getting a "No route matches "/clients//user" with {:method=>:post}" Even tho i checked my routes and i do HAVE "POST /clients/:client_id/user(.:format) {:action=>"create", :controller=>"users"}"
Gotjosh
A: 

I solved this by using nested attributes, by including the user model, when creating the client. And it works flawlessly.

In case any of you guys need more info here's the two screencasts that helped me come up with as solution:

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/196-nested-model-form-part-2

Gotjosh