Damilola Lucas Aromiwe Damilola Lucas Aromiwe - 6 months ago 8
Ruby Question

When I try to load my create.html.erb page, I get this error "param is missing or the value is empty"

I got this error on loading the page and I would guess that it has somethinng to do with my create method in the controller
My controller looks like this

class StoryController < ApplicationController
def index
@story = Story.all
end

def new
@story = Story.new
end

def create
@story = Story.new(story_params)
if @story.save
flash[:notice] = "Story created successfully"
flash[:color]= "valid"
else
flash[:notice] = "Story is invalid, man"
flash[:color]= "invalid"
end

end

def show
@story = Story.find(params[:id])
end
private
def story_params
params.require(:story).permit(:story_title, :story_body)
end

end


My create.html.erb looks like

<%= form_for @story ,url: story_path do |f| %>
<%= label :story, :title %><br />
<%= text_field :story, :story_title %>

<%= label :story, :body %><br />
<%= text_field :story, :story_body %>
<%= submit_tag 'Create story' %>
<% end %>


My create.html.erb didnt look like this before, I changed it to that after I read some questions about how form_for would work instead of form_tag for the story_params.
But either way, I still get the error anyways and I would like to know why and if there is a fix for it.

Answer

The create method is a POST... so when he enters story/create he's already expecting those values... that's why he says he can't find the params... i didn't look at the code deeply but it seems fine. Just change the name of the view to new.html.erb. New is the setup for create.

In new you setup the values and then invoke create where the controller actually creates the story.

Just change the name of the view to new.html.erb and change it to this

<%= form_for @story do |f| %>
<%= f.label :title %><br />
<%= f.text_field :story_title %>
<%= f.label :body %><br />
<%= f.text_field :story_body %>
<%= submit_tag 'Create story' %>
<% end %>

as the other user said. you need to say that those inputs belong to form |f| the text_field belongs to f, f.text_field

And of course you access that view through stories/new