Vito
Vito

Reputation: 786

Routing in Rails with view and new action

I have the following question:

i have a controller called: companies_controller in which i have the following actions:

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

Also , i have this view:

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

if i cut off the

@company = Company.new

from the new action , nothing works. Why? in others project i used the new action without to create a new object. Can you explain me , why in this case it doesn't work?

this is my routes:

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

Upvotes: 0

Views: 46

Answers (1)

Sajjad Murtaza
Sajjad Murtaza

Reputation: 1502

Please try this in routes

resources companies

and form

form_for @company  do |f|

Upvotes: 1

Related Questions