0

I have a table Company with companyid and companyname, and a table Config for my company configuration that have id, companyid, companyname, config that is in my dropdown generate by rails g scaffold

how can I override/change Ruby's Save/create button on creating my new config? Is it right to have my companyname on the config table or is it better to remove it and have a method geyCompanyName(id)?

I'm new to Ruby and just trying to follow the tutorial

Controller looks like

 def index
@company_config = CompanyConfig.all

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @company_config }
end
end


def show
@@company_config = CompanyConfig.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @company_config }
end
end


 def new
@company_config = ComapnyConfig.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @company_config }
end
end


def edit
 @company_config = CompanyConfig.find(params[:id])
end

def create
@company_config =  CompanyConfig.new(params[:company_config])

respond_to do |format|
  if @company_config.save
    format.html { redirect_to @company_config, notice: 'Config was successfully created.' }
    format.json { render json: @company_config, status: :created, location: @company_config }
  else
    format.html { render action: "new" }
    format.json { render json: @@company_config.errors, status: :unprocessable_entity }
  end
  end
  end

I tried to look on Controller create but not sure how to edit it to meet what I want to accomplish.

my view looks like

<%= simple_form_for(@company_config) do |f| %>
 <%= f.error_notification %>

 <div class="form-inputs">
  <%= f.input :companyid, collection:build_company_select_array %>
  <%= f.input :companyname %>


 </div>

 <div class="form-actions">
  <%= f.button :submit %>
  </div>
Archie Reyes
  • 527
  • 2
  • 14
jayj
  • 23
  • 6
  • There's too little information to answer the question. What have you tried? What didn't work? What does your view look like? What does your controller look like? – Jesper Mar 02 '15 at 14:38
  • You should tag this as ruby-on-rails, not ruby. – Kris Mar 02 '15 at 15:19

1 Answers1

0

You should NOT have information like companyname stored in two places.

The only thing you need in your CompanyConfig model is the id, the company_id, and any other information you want to store about the company that's appropriate to a configuration record.

And you can have Company defined so that it has_one company_config (or has_many company_configs) and CompanyConfig defined so it belongs_to company

When you want to reference the company name, it's accessible by my_company_config.company.companyname although you'll eventually learn to delegate methods. A very simple example...

class CompanyConfig
  belongs_to company
  def companyname
    company.companyname
  end
end

... which will give you the ability to do my_company_config.companyname but still store the data in only one place.

SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53