Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difficulty adding custom form action #175

Open
ryanb opened this issue Mar 12, 2020 · 0 comments
Open

Difficulty adding custom form action #175

ryanb opened this issue Mar 12, 2020 · 0 comments
Labels

Comments

@ryanb
Copy link

ryanb commented Mar 12, 2020

I'm attempting to add an extend_trial action to AccountsController. I added a route:

resources :accounts do
  member do
    get :extend_trial
  end
end

And the controller action which has the same behavior as the edit action:

def extend_trial
  edit
end

And then I copied the edit view to extend_trial.html.erb with one field:

<%
# decorate first on every page or partial
decorated = decorate resource
%>
<article class="resource">
  <div class="resource__header">
    <h1><%= t 'labels.edit', id: decorated.id %></h1>
    <%= render 'resource_navs' %>
  </div>
  <%= render 'flash_messages' %>
  <section>
    <div class="resource__container">
      <%= form_for decorated, url: url_for(action: :show), method: :put, html: { multipart: true }, builder: Wallaby::FormBuilder do |form| %>
        <% decorated.errors[:base].try do |messages| %>
          <% Array(messages).each do |message| %>
            <div class="alert alert-danger alert-dismissible" role="alert">
              <%= raw message %>
            </div>
          <% end %>
        <% end %>
        <%= type_render decorated.form_type_of("trial_end_date"), field_name: "trial_end_date", form: form %>
        <%= form.button t('buttons.save'), class: 'btn btn-primary' %>
        <%= cancel_link html_options: { class: 'btn btn-default' } %>
      <% end %>
    </div>
  </section>
</article>

This produced the following error:

undefined method `extend_trial_metadata_of' for #<SupportAdmin::AccountDecorator:0x00007ff815720078>

It took some digging to realize there's a form_metadata_of method which I assume is the behavior I want so I aliased it:

# in AccountDecorator
alias_method :extend_trial_metadata_of, :form_metadata_of

Then I get this error:

ActionView::MissingTemplate - Missing partial /extend_trial/_datetime, /_datetime, support_admin/accounts/extend_trial/_datetime, support_admin/accounts/_datetime, support_admin/application/extend_trial/_datetime, support_admin/application/_datetime, wallaby/resources/extend_trial/_datetime, wallaby/resources/_datetime with {:locale=>[:"en-US"], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
  * "/Users/rbates/code/envisage/app/views"
  * "/Users/rbates/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/kaminari-core-1.1.1/app/views"
  * "/Users/rbates/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/ckeditor-4.2.4/app/views"
  * "/Users/rbates/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/wallaby-6.1.0/app/views"
  * "/Users/rbates/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/devise_invitable-1.7.4/app/views"
  * "/Users/rbates/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/devise-4.5.0/app/views"

So I add a partial at accounts/_datetime.html.erb which delegates to the internal date time partial:

<%= render partial: "wallaby/resources/form/datetime", locals: local_assigns %>

The form now renders (yay) but doesn't look quite right since the body tag doesn't have an edit class due to a different action name. This would be resolved by not nesting CSS as mentioned in #174.

When I submit the form I get this error:

NameError - undefined local variable or method `form' for #<#<Class:0x00007fc934466f18>:0x00007fc93bfdb068>
Did you mean?  fork:
  app/views/support_admin/accounts/_datetime.html.erb:1:in `_app_views_support_admin_accounts__datetime_html_erb__554914958012286755_70251162594040'

The logs show the update action worked and redirected to the show action which resulted in this error. It appears the custom datetime partial I added is being used by the show action but does not have a form variable. It is confusing to have two partials with the same name that have different behavior and locals. I suggest renaming the the form partials to datetime_field and moving them out of the form directory. Flat lists with unique names are good for customizability.

I moved the partial to accounts/extend_trial/datetime which resolves the error and renders the page.

Overall this was quite difficult to figure out. I suggest smoothing out this process and adding some documentation for customizing Wallaby in this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants