ruby on rails - Using nested routes and nested attributes together (and optionally) -
trying pretty straightforward.
i have system have clients have tasks have files , notes
i can create task not related 1 particular client.
i want able create new task, client screen , populate client field (or have populated in background) wihtout having select it. of course, i'll need client selector if create task , want attach client it.
hope makes sense
i have nested route follows:
resources :clients resources :notes resources :tasks resources :notes resources :task_files end end resources :tasks resources :notes resources :task_files end and load of stuff relating nested attributes etc works.
the 1 thing can't quite work out how use same _form when i'm creating task, , when i'm creating task when know client. e.g. clients/:id/tasks/new vs tasks/new
my gut feel if put @project _form la:
<%= simple_nested_form_for [@client,@task], html:{multipart: true} |f| %> then it's not going work tasks without clients whereas:
<%= simple_nested_form_for @task, html:{multipart: true} |f| %> doesn't preselect client in:
<%= f.input :client_id, :collection => client.accessible_by(current_ability, :read), :as => :select, :label => "client" %> which used if there no client preselected.
any ideas how work, maybe being able use parameter @client.id in way? have tried @client, @client.id , :client_id no joy.
am sure must pretty simple...
to clarify: if client present (i.e. known through url) i'd preselect client , not allow changed. if client id not present, i'd user able select client applies (or leave blank).
it sounds creating task form, , never creating client, use
<%= simple_form_for @task |f| %> and include inputs making new task. in form making task, can check whether user instance variable of @user defined not. if you're @ nested route, should defined, , can hidden field assign client_id.
<% if defined?(@user) %> <%= f.hidden_field, :client_id, value: @user.id %> <% else %> <%= f.input :client_id, :collection => client.accessible_by(current_ability, :read), :as => :select, :label => "client", :value_method => :id %> <% end %>
Comments
Post a Comment