Question

I can't find what I did wrong here, is there something I'm missing?

My new action/view here:

<h1>Submit a new experiment here!</h1>

<%= render "form" %>

My _form.html.erb form partial

<h1>THis is a form</h1>

<%= form_for(@experiment) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </div>
  <div class="field">
    <%= f.label :plan %><br />
    <%= f.text_field :plan %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

My experiments controller:

class ExperimentController < ApplicationController

    def index
    end

    def new
    @experiment = Experiment.new
    end

end

And my routes:

  devise_for :users
  resources :home
  resources :experiment

  root to: "home#index"

I am visiting the following url:

http://localhost:3000/experiment/new
Was it helpful?

Solution

In you want to follow rails convention then:

  1. Rename your controller from ExperimentController to ExperimentsController, i.e plural Experiments.
  2. Rename your controller file name app/controllers/experiment_controller.rb to app/controllers/experiments_controller.rb, i.e. plural experiments_controller.rb.
  3. Update your routes by modifying resources :experiment to resources :experiments.

If not, update your form_for call as:

<%= form_for(@experiment, url: url_for(controller: 'experiment', action: 'create')) do |f| %>
  ...
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top