Search code examples
ruby-on-rails-3routescustom-routes

Ruby on Rails: Custom Actions (Follow up)


This question is a follow up to this previous question: Ruby on Rails: Custom actions

As a follow up, what would be the syntax to use a custom action in a form_for? For my app, I have a partial called _invite_form.html.erb, and set the form to have a :url specification that I thought would link the form to the invite action on the Users controller:

 <div id = "invite_form">
<h1>Invite</h1> 
<%= form_for(invited, :url => invite_user_path) do |f| %>
    <div class="field">
        <%= f.text_field :email, :class => "inputform round", :placeholder => "email" %>
    </div>
    <div class="actions">
        <%= f.submit "Invite", :class => "submit_button round" %>
    </div>  
<% end %>
 </div>

This partial is called on certain pages, and this error is given:

  "No route matches {:action=>"invite", :controller=>"users"}"

In my routes.rb file I have included the appropriate lines:

 resources :users do
     member do
      get :invite
      post :invite
    end
 end

Why is it that the route doesn't work? How do I change these files to make the form use the action "Invite" on the Users controller?

** Forgot to mention earlier: I defined invited in the Users helper: users_helper.rb:

 module UsersHelper
   def invited
     @invited = User.new(params[:user])
   end
 end

Solution

  • As you don't have a persistent User just yet, make this a collection operation by:

    1. Changing invite_user_path to invite_users_path in your controller
    2. Changing member do to collection do in your routes