Changing routes after adding admin functionality with devise

37 Views Asked by At

This question is going to be somewhat convoluted because I am new to RoR but got thrown into a relatively large RoR project for my class. For that, I apologize, but I'll do my best to be clear. I readily admit we may be doing this horribly wrong, to begin with.

So anyways, here's the situation:

  • We originally tried to revive some legacy code built by a professional company and got close, but ultimately had no luck.

  • Because of this we started fresh on a new project and used scaffolding to build the structure of our project.

  • We used a decent amount of code from the legacy project but ended up adding a LOT more than we actually use. Now we aren't really sure what's needed and what isn't.

  • Long story short, we have a working project, but recently tried to separate our functionality by user and admin using devise (I'm not exactly sure to what extent). In doing so we separated our admin controllers and views into an admin subfolder such that we then had "/controllers/admin/events" and "/controllers/events" and the same for our views. The idea being that we then have a separate view for our admins and our users (again, fairly certain this isn't the proper way to do this).

I am almost certain that we are doing this wrong, but it has ultimately led me to this question:

We changed our routes.rb to include our resources under an admin namespace like this:

namespace :admin do
    resources :form_defs do
        resources :questions
    end
end

But when I try to do something like "link_to @form_defs" like we originally did, it still tries to route to form_defs_questions_path instead of admin_form_defs_questions_path

I think my question is simply how do I change the class such that it routes appropriately?

If there is a better way we should have done this that doesn't involve basically starting the project over, I'm all ears.

I think I need to change where my object routes to by default:

The code I'm having issues with is a form generator that creates a new question. This is the code we are using for this:

<div id="new-question-form">
  <h2 id="add-question-title">Add Question</h2>
  <%= form_for([@form_def,@form_def.questions.build]) do |f| %>

      <div class="new-question-field">
        <strong><%= f.label :format %></strong>
        <%= f.select(:format, [["Short Answer","1"]]) %>
      </div>

      <div class="new-question-field">
        <strong><%= f.label :title %></strong>
        <%= f.text_field :title %>
      </div>

      <div class="actions">
        <%= f.submit "Add Question", :class => "small-gray-button", :id => "add-question-button"%>
      </div>
  <% end %>
</div>

The specific line causing issues is this:

<%= form_for([@form_def,@form_def.questions.build]) do |f| %>

I looked up the documentation for "form_for()", but being new to RoR, the explanation didn't help much.

So when I get to this point in the code, it apparently tries to route to "form_def_questions_path" instead of "admin_form_def_questions_path". I have tried making it:

<%= form_for([admin_form_def_questions_path(@form_def),@form_def.questions.build]) do |f| %>

But it seems to require an object as input, not a path.

Again, I realize there are probably better ways to do this, so if you have a suggestion I'm all ears. Thanks!

2

There are 2 best solutions below

1
On

You can write full router admin_form_defs_questions_path(@form_defs) or you can define namespace link_to [:admin, @form_defs]

1
On

Worked on a similar project like that. And we approached the admin/user interfaces the same way.

What you'll need to start doing is to stop relying on rails to guess the things for you, and specify them manually.

For example

<%= render  @form_defs %>
<%= link_to @form_defs %>

Will become something like:

<%= render partial: 'admin/form_defs/questions', locals: {form_defs: @form_defs} %>
<%= link_to admin_form_defs_questions_path(@form_defs) %>

This gives you good control over the split between what user can see/do and what admin can see/do