简体   繁体   中英

How to render different forms for various account types in ruby on rails?

Multiple Devise Registration Form Views

I assume that the entire format of the injected ruby in the first row div, is incorrect. I included it to display the number of devise registration views needed. I also included the error, knowing it was obvious.

Anyone care to take this opportunity to exemplify proper RoR format for multiple if, elsif, when, etc... statements? I love all opinions.

Please excuse and correct any incorrect terminology that you see fit.

Thank you


Error: SyntaxError in Devise::RegistrationsController#new


devise/registrations/new.html.erb

<div class="row">
  <%= if params[:plan] == '1' %>
      Contributor
  <%= elsif params[:plan] == '2' %>
      Elite Contributor
  <%= elsif params[:plan] == '3' %>
      Technician
  <%= elsif params[:plan] == '4' %>
      Elite Technician
  <%= elsif params[:plan] == '5' %>
      Center
  <%= elsif params[:plan] == '6' %>
      Elite Center
  <%= elsif params[:plan] == '7' %>
      Affair
  <%= elsif params[:plan] == '8' %>
      Elite Affair
<% end %>

  <div class="col-md-4 col-md-offset-4">
    <div class="well">
      <div class="page-header text-center">
        <h2>JOIN</h2>
        <li role="separator" class="divider"></li>
      </div>

      <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
        <%= devise_error_messages! %>

        <div class="field form-group">
          <%= f.label :email %><br />
          <%= f.email_field :email, autofocus: true, class: 'form-control' %>
        </div>

        <div class="field form-group">
          <%= f.label :password %>
          <% if @minimum_password_length %>
          <em>(<%= @minimum_password_length %> characters minimum)</em>
          <% end %><br />
          <%= f.password_field :password, autocomplete: "off", class:'form-control' %>
        </div>

        <div class="field form-group">
          <%= f.label :password_confirmation %><br />
          <%= f.password_field :password_confirmation, autocomplete: "off", class:'form-control' %>
        </div>

        <br>

        <div class="actions text-center">
          <%= f.submit " JOIN ", class:'btn btn-lg btn-success' %>
        </div>
      <% end %>

      <div class="text-right">
        <%= render "devise/shared/links" %>
      </div>

    </div>
  </div>
</div>

controller/pages_controller.rb

def home
    @contributor_plan = Plan.find(1)
    @elitecontributor_plan = Plan.find(2)
    @technician_plan = Plan.find(3)
    @elitetechnician_plan = Plan.find(4)
    @center_plan = Plan.find(5)
    @elitecenter_plan = Plan.find(6)
    @affair_plan = Plan.find(7)
    @eliteaffair_plan = Plan.find(8)
end

pages/home.html.erb

(one of the many ruby links on my home page view)

<%= link_to "Contributor", new_user_registration_path(plan: @contributor_plan.id), class:'btn btn-info btn-lg btn-block' %>

The first thing I see is that you are using <%= %> erb tags rather than <% %> for if/elsif. This is unnecessary and potentially problematic. Try taking out the =.

Some additional thoughts:

This would be a good place for a case statement rather than if conditionals.

It is also considered best practice in Rails to pull out large chunks of code like this into a helper method, rather than putting it in the view. This is often not simply a matter of style, as it will make it easier to debug.

Finally, it's not entirely clear what you are trying to do here. It looks to me like the only thing the code is doing is displaying different text above the form, and the form below is not affected by the erb code. This makes me suspect there is a simpler and less error-prone way to do what you are doing. The hard-coded links and instance variables for the various plans are red flags too. I would try refactoring the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM