phuck
phuck

Reputation: 15

Rails app only adding "created_at" and "updated_at" to DB

(using sqlit3)

I've just succeeded in making form submission work- however, all the fields aren't submitted. Only Rails' default "updated_at" and "created_at" values are.

Code is in this post.

Upvotes: 0

Views: 162

Answers (2)

chrisgeeq
chrisgeeq

Reputation: 149

Add this to the Recipe Controller

def recipe_params
 recipe_params = params.require(:recipe).permit(:name,:recipe)
end

this method allows you select parameters to be added

create action as usual

def create
 @recipe = Recipe.create(recipe_params)
 if @recipe.save
    redirect_to recipe_new_path
 else
    reload_page
 end
end

then your form should look like this,pay attention to the :recipe symbol

<%= form_for :recipe do |f| %>
<%= f.label :name, "Recipe Name:" %>
<%= f.text_field :name %>
<br>
<%= f.label :recipe, "Recipe Description:" %>
<%= f.text_field :recipe %>
<br>
<%= f.submit %>
<% end %>

Note in your form,you had this @recipe instead of :recipe,hence that resulted in the missing param error message,just make sure you have the same variable in the controller and form.

Upvotes: 0

Surya
Surya

Reputation: 2699

You need to permit the attributes that can be used by the controller for updation/creation. In your case you need to permit the fields name and recipe.

In your controller methods,change recipe_params like this:

def recipe_params
    recipe_params = params.require(:recipe).permit(:name,:recipe)
  end

and in your create method, call the method recipe_params instead of directly using params[:recipe] to create. i.e:

 def create
    @recipe = Recipe.create(recipe_params)
    if @recipe.save
        redirect_to recipe_new_path
    else
        reload_page
    end
 end

This should work.

Upvotes: 1

Related Questions