【问题标题】:Rails: How to make a form post to another controller actionRails:如何将表单发布到另一个控制器操作
【发布时间】:2013-06-09 00:50:37
【问题描述】:

我知道您通常应该在 rails 中使用 new/create 和 edit/update 之间的链接,但我有一个案例需要其他东西。无论如何我可以实现同样的连接吗?

我有一个模型的表单,我希望它发布数据(类似于新视图发布到创建操作的方式)。

这是我的表格

<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>

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 forms


    【解决方案1】:

    使用:url 选项。

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

    【讨论】:

    • = form_for @job, :url =&gt; company_path, :method =&gt; :post/:put
    • 对于收到此错误undefined method model_name for NilClass:Class 的任何人,请使用:= form_for Job.new, url: company_path, method: :post
    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    相关资源
    最近更新 更多