简体   繁体   中英

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.

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 . ie:

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

This should work.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM