使用视图和新操作在Rails中路由

时间:2015-08-10 14:53:44

标签: ruby-on-rails view controller routes action

我有以下问题:

我有一个名为companies_controller的控制器,我在其中执行以下操作:

class CompaniesController < ApplicationController

    #before_filter :set_company, only: [:show, :edit, :update, :destroy]
    # GET /companies
  def index
    @companies = Company.all
  end

  # GET /companies/new
  def new
    @company = Company.new
  end

  # POST /companies
  def create
    @company = Company.new(company_params)

    respond_to do |format|
            if @company.save
            format.html { redirect_to @company, notice: 'Azienda creata.' }
            format.json { render :show, status: :created, location: @company }
          else
            format.html { render :new }
            format.json { render json: @company.errors, status: :unprocessable_entity }
          end
    end
  end

另外,我有这个观点:

<%= form_for @company ,:url => {:action => :create, :controller => :companies, :method => :post} do |f| %>
  <% if @company.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@company.errors.count, "error") %> prohibited this order from being saved:</h2>

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

  <div class="field">
    <%= f.label :RagioneSociale %><br>
    <%= f.text_field :ragione_sociale, size: 40 %>
  </div>


  <div class="actions">
        <!-- Quando eseguiamo l'ordine facciamo il render di _line_item_simple.html perchè nella mail non posso mettere
                 button_to -->
    <%= f.submit 'Completa' %>
  </div>
<% end %>

如果我切断了

@company = Company.new

来自新动作,没有任何作用。 为什么?在其他项目中,我使用新动作而不创建新对象。 你能解释一下,为什么在这种情况下它不起作用?

这是我的路线:

  get "companies/index"
  get "companies/new"
  post "companies/create"

1 个答案:

答案 0 :(得分:1)

请在路线

中试试
resources companies

和表格

form_for @company  do |f|