Ruby-on-rails – Rails: How to make a form post to another controller action

formsrubyruby-on-railsruby-on-rails-3

I know you are usually supposed to use the linking between new/create and edit/update in rails, but I have a case where I need something else. Is there anyway I can achieve this same connection?

I have a form for a model and I want it to post the data (similar to how a new view, post to the create action).

Here is my form

<div id="new-job">  
  <%= form_for(@job) do |f| %>
    <% if @job.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@job.errors.count, "error") %> prohibited this job from being saved:</h2>

        <ul>
        <% @job.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name %>
    </div>
    <div class="field">
      <%= f.label :location %><br />
      <%= f.text_field :location %>
    </div>
    <div class="field">
      <%= f.label :description %><br />
      <%= f.text_area :description %>
    </div>
    <div class="actions">
      <%= f.submit "create job", id: "submit-job", class: "button small radius" %>
      <%= link_to "go back", jobs_path, class: "button small radius secondary" %>
      <div id="new-job-errors"> </div>
    </div>
  <% end %>
</div>

Best Solution

Use the :url option.

= form_for @job, :url => company_path, :html => { :method => :post/:put }