4

I am trying to build a simple_form_for in Rails 3.2 for @objects with a doubly nested route, e.g.:

/users/1/projects/2/objects

The form is:

<%= simple_form_for @object, :url => user_project_objects_path(@user, @project), :html => { :class => 'form-horizontal' } do |f| %>

In routes.rb:

resources :users do
  resources :projects do
    resources :objects do
      collection { post :import }
    end
  end
end

My question is: what goes in the new and create action of the Object controller?

So far - and I am getting a routing error - I have:

def create
  @user = current_user
  @project = Project.find_by_user_id(@user)
  @object = @project.objects.build(params[:object])
  if @object.save
    flash[:notice] = "Object was successfully created"
    redirect_to user_project_objects_path
  else
    render 'new'
  end
end

def new
  @user = current_user
  @project = Project.find_by_user_id(@user)
  @object = @project.objects.build
end

Any advice is much appreciated.

In response to Greg W:

No route matches {:action=>"edit", :controller=>"objects", :user_id=>#<Object id: 8, source_lang_id: 1, source_content: "Kaffehaus", target_lang_id: 2, target_content: "cafe", domain_id: 2, owner_id: nil, created_at: "2013-01-04 06:36:55", updated_at: "2013-01-04 06:36:55", project_id: 2>}

The owner_id (User, i.e. current_user) is not being updated - and this may be the issue(?)

Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70
codervince
  • 316
  • 4
  • 15
  • Can you provide the routing error? It might be a result of your url helper `user_project_objects_path` after the `redirect_to`. It likely requires an argument to specify the project id. i.e., `user_project_objects_path(@project)`. – Greg W Jan 04 '13 at 05:01
  • thanks for your reply. Updated with routing error – codervince Jan 04 '13 at 06:41

2 Answers2

5

In the form you can use

simple_form_for([@user, @project, object]) do

You can use the relationship to fetch data like

@project= current_user.projects

instead of

@project = Project.find_by_user_id(@user)
theIV
  • 25,434
  • 5
  • 54
  • 58
Hitesh
  • 51
  • 1
  • current_user.projects would return an array of projects. I am only interested in the project I am currently in. The simple_form_for header is equivalent to the suggestion above. I believe the answer lies in how the New model builds the three instance variables and how they are updated in create Object to include the user_id and project_id in the params hash. – codervince Jan 04 '13 at 07:04
0

I worked it out in the meantime. Perhaps this may be a usual guide to others working on a doubly nested routing such as user -> projects -> objects (has_many, has_many)

In the Object controller New action:

@user = current_user
@project = Project.find(params[:project_id])
@object = Object.new

In the Object controller Create action:

 @user = current_user
 @project = Project.find(params[:project_id])
 @object = @project.objects.build(params[:object])
 @object.owner_id = @user.id  #owner id maps to User

The routing error was a result of not specifying all objects, user, project and objects in

user_project_objects_path(@project)
codervince
  • 316
  • 4
  • 15