문제

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 %>
도움이 되었습니까?

해결책 2

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

다른 팁

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| %>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top