4

I have two forms for new and edit that I want to extract to a partial, but, to my begginer knowledge in Rails, they need different paths in order to work.

This is my edit form:

<%= simple_form_for [@user, @wiki], url: user_wiki_path do |f| %>
  <%= f.input :title %>
  <%= f.input :body %>
  <%= f.submit class: "btn btn-success"  %>
<% end %>

This is my new form:

<%= simple_form_for [@user, @wiki], url: user_wikis_path, method: :post do |f| %>
  <%= f.input :title %>
  <%= f.input :body %>
  <%= f.submit class: "btn btn-success"  %>
<% end %>

Can I, and if so, how, combine them into one partial _form.html.erb conditionally specifing the paths?

When I try joining them now I get the error below when trying to edit it:

No route matches [PATCH] "/users/32/wikis"

These are my routes:

                      Prefix Verb   URI Pattern                              Controller#Action
             users_index GET    /users/index(.:format)                   users#index
              users_show GET    /users/show(.:format)                    users#show
                    root GET    /                                        pages#index
        new_user_session GET    /users/sign_in(.:format)                 devise/sessions#new
            user_session POST   /users/sign_in(.:format)                 devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                devise/sessions#destroy
           user_password POST   /users/password(.:format)                devise/passwords#create
       new_user_password GET    /users/password/new(.:format)            devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)           devise/passwords#edit
                         PATCH  /users/password(.:format)                devise/passwords#update
                         PUT    /users/password(.:format)                devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                  devise/registrations#cancel
       user_registration POST   /users(.:format)                         devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                 devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                    devise/registrations#edit
                         PATCH  /users(.:format)                         devise/registrations#update
                         PUT    /users(.:format)                         devise/registrations#update
                         DELETE /users(.:format)                         devise/registrations#destroy
              user_wikis POST   /users/:user_id/wikis(.:format)          wikis#create
           new_user_wiki GET    /users/:user_id/wikis/new(.:format)      wikis#new
          edit_user_wiki GET    /users/:user_id/wikis/:id/edit(.:format) wikis#edit
               user_wiki PATCH  /users/:user_id/wikis/:id(.:format)      wikis#update
                         PUT    /users/:user_id/wikis/:id(.:format)      wikis#update
                         DELETE /users/:user_id/wikis/:id(.:format)      wikis#destroy
                   users GET    /users(.:format)                         users#index
                         POST   /users(.:format)                         users#create
                new_user GET    /users/new(.:format)                     users#new
               edit_user GET    /users/:id/edit(.:format)                users#edit
                    user GET    /users/:id(.:format)                     users#show
                         PATCH  /users/:id(.:format)                     users#update
                         PUT    /users/:id(.:format)                     users#update
                         DELETE /users/:id(.:format)                     users#destroy
                   wikis GET    /wikis(.:format)                         wikis#index
                    wiki GET    /wikis/:id(.:format)                     wikis#show
                 charges POST   /charges(.:format)                       charges#create
              new_charge GET    /charges/new(.:format)                   charges#new
                  charge PATCH  /charges/:id(.:format)                   charges#update
                         PUT    /charges/:id(.:format)                   charges#update
Hugo Carlos
  • 401
  • 3
  • 22

2 Answers2

1
# _form.html.erb    
<%= simple_form_for @wiki do |f| %>
  <%= f.input :title %>
  <%= f.input :body %>
  <%= f.submit class: "btn btn-success"  %>
<% end %>

and on your wikis#edit and wikis#new, add

...
@user = User.find params[:user_id]
...
pauloancheta
  • 349
  • 2
  • 9
  • Tryed that... Unfortunatly I get the error No route matches [POST] "/wikis"... Thanks for the help nonetheless. – Hugo Carlos Apr 30 '15 at 20:58
  • 2
    This will work. It's giving you the No route matches [POST] error because it's nested under a User model. You need to pass both the User and the Wiki. ```<%= simple_form_for([@wiki.user, @wiki]) do |f| %>``` should do it. – Alex S. May 01 '15 at 14:40
1

You can post local variables into the partial. Something like the following would work:

new.html.erb

<%= render partial: '_form', locals: {url: user_wikis_path, method: :post} %>

edit.html.erb

<%= render partial: '_form', locals: {url: user_wiki_path(@user, @wiki), method: :post} %>

_form.html.erb

<%= simple_form_for [@user, @wiki], url: url, method: method do |f| %>
  <%= f.input :title %>
  <%= f.input :body %>
  <%= f.submit class: "btn btn-success"  %>
<% end %>

Or you could set @url and @method as instance variables and access them that way.

Controller

def new
  @method = :get
  @url = user_wikis_path
  ...
end

def edit
  @method = :post
  @url = user_wiki_path(@user, @wiki)    
  ...
end

_form.html.erb

<%= simple_form_for [@user, @wiki], url: url, method: method do |f| %>
  ...

Or you could have a conditional in the form that accesses the current action, if you use action_name in the view it'll return the name of the current action.

  • Tryed your first code, but I get the error: SyntaxError in WikisController#new /Users/hugocbp/Code/rails_apps/blocipedia/app/views/wikis/new.html.erb:1: syntax error, unexpected tSTRING_BEG, expecting ')' ...ffer.append=( render :partial '_form', locals: {url: user_wi... ... ^ /Users/hugocbp/Code/rails_apps/blocipedia/app/views/wikis/new.html.erb:1: syntax error, unexpected ',', expecting ')' ...pend=( render :partial '_form', locals: {url: user_wikis_pat... ... ^ I want to try your last option but I don't know how... – Hugo Carlos Apr 30 '15 at 20:59
  • I can't see an error in my example, though there might be one, and I can't read your error message very well. It's a better way of doing it. I'll add instructions for the other method to my example – Mike Hefferan May 05 '15 at 15:03
  • Ah - I spotted the problem in my original code, the first example should work now – Mike Hefferan May 05 '15 at 15:10