Form_for not saving data to database
Just do like:
def create
@reg = Register.new(params.required(:register).permit(:user_id, :class_name))
@reg.user_id = current_user.id
@reg.save! # This will raise exception if something is wrong
end
Btw, just apply to TDD/BDD concepts to eliminate problems like this one. This process is pretty simple.
rails form data not getting saved to db
if you are using form_for
, use the form_for
syntax
<%= form_for(@calculate) do |form| %>
<%= form.label :number %>
<%= form.text_field :number %>
<%= form.label :root %>
<%= form.text_field :root %>
<%= form.submit "Submit" %>
<% end %>
this will automatically handle the routes if the @calculate
is new object it will submit it to create or if it is already saved it will send a put request to edit action
Form fields are not saved into database - ruby on rails
new
is the wrong method in which to perform #save
. that should be done in create
def new
@landslide = Landslide.new
end
def create
@landslide = Landslide.new(landslide_params)
if @landslide.save
render plain: @landslide.inspect
else
render :new
end
end
Also, your form looks wrong. The location of the form data in your returned params depends on the control's name
field, not on the id
Instead of
%input#location.form-control{:type => "Location"}
I'd expect to see
%input#landslide_location.form-control{:type => "text", :name => "landslide[location]"}
Form fields not saving to DB: does this have to do with how form parameters are passed for new/create controller methods?
What is the correct way to pass in parameters for the methods
‘create' and ‘new’?
Your form looks good for me.
is there anything else in the code given here that might be preventing
posts from being saved to DB?
Your create
action doesn't save a record.
def create
@post = Post.new(post_params)
@post.save
redirect_to '/'
end
The errors I see are something about missing.png — I’m assuming this
is because there is no default image loaded, one called “missing.png”.
Right
Related Topics
Omission of Curly Braces for a Hash in an Array
Why Doesn't Array Override the Triple Equal Sign Method in Ruby
Rails Postgresql Multiple Schemas and the Same Table Name
Conditional Dependency in Ruby Gemspec
What Does "String Literal in Condition" Mean
How to Effectively Force Minitest to Run My Tests in Order
Empty Strings at the Beginning and End of Split
How Does Sinatra Define and Invoke the Get Method
Permanently Switching User in Capistrano 3 (Separate Authorization & Deploy)
Ruby Getting Deeply Nested JSON API Data
Wicked_Pdf Is Not Rendering Header
How to HTML_Escape Text Data in a Sinatra App
Why Do Numeric String Comparisons Give Unexpected Results
Undefined Method Error When Creating Delayed_Job Workers with Script/Delay_Job
Why Is Autoload Failing to Load Files for Gems