ruby on rails - Form fields not saving to DB: does this have to do with how form parameters are passed for new/create controller methods? -


i’m newbie trying create simple rails (4.2.1) app allows user (when logged in >> ‘devise’ gem) add post image (‘paperclip’ gem).

the form data not being saved in db. suspect might have way parameters passed since i’m new this, i’m not sure.

it working — posts being saved in db , displayed on site — until refactored form use @post instead of post.new (so view isn’t speaking directly model), , added 'new' method controller, form data not being saved.

i tried both using , omitting passing in ‘post_params’ 'new' method. when pass in params 'new' method, error:

actioncontroller::parametermissing (param missing or value empty: post):   app/controllers/posts_controller.rb:22:in `post_params'   app/controllers/posts_controller.rb:18:in `new' 

as side note: left out “multipart: true” in form because based on rails documentation, understood needed “form_tag” , not “form_for” (http://guides.rubyonrails.org/form_helpers.html).

here controller:

class postscontroller < applicationcontroller   before_action :authenticate_user!, :except => [:index, :show]    def index      @post = post.all   end    def show     @post = post.find(params[:id])   end    def create     @post = post.new(post_params)     redirect_to '/'   end    def new     @post = post.new   end    def post_params     params.require(:post).permit(:caption, :image)   end end 

here form (posts/new.html.erb):

<%= form_for @post  |f| %>   <%= f.label :caption %>   <%= f.text_field :caption %>   <%= f.file_field :image %>   <%= f.submit %> <% end %>  <br><a href=/posts> posts </a> 

unfortunately refactored form @post @ same time added image upload form; before this, form working fine , data being stored in db , displayed on site correctly (and "correct" mean intended :)

after starting sever , loading page locally, i’m not seeing errors give me clues saving form data db. errors see missing.png — i’m assuming because there no default image loaded, 1 called “missing.png”.

started "/images/medium/missing.png" ::1 @ 2015-07-14 12:06:59 +0200  actioncontroller::routingerror (no route matches [get] "/images/medium/missing.png"):   actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 

edited here include post model:

class post < activerecord::base   belongs_to :user   has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"   validates_attachment_content_type :image, :content_type => /\aimage\/.*\z/ end 

this db schema:

  create_table "posts", force: :cascade |t|     t.text     "caption"     t.datetime "created_at",         null: false     t.datetime "updated_at",         null: false     t.string   "user_id"     t.string   "image_file_name"     t.string   "image_content_type"     t.integer  "image_file_size"     t.datetime "image_updated_at"   end    create_table "users", force: :cascade |t|     t.string   "email",                  default: "", null: false     t.string   "encrypted_password",     default: "", null: false     t.string   "reset_password_token"     t.datetime "reset_password_sent_at"     t.datetime "remember_created_at"     t.integer  "sign_in_count",          default: 0,  null: false     t.datetime "current_sign_in_at"     t.datetime "last_sign_in_at"     t.inet     "current_sign_in_ip"     t.inet     "last_sign_in_ip"     t.datetime "created_at"     t.datetime "updated_at"   end    add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree   add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree 

my questions are:

— correct way pass in parameters methods ‘create' , ‘new’?

— if not in fact cause of error, there else in code given here might preventing posts being saved db?

bonus question:

the current rspec failure not detailed (probably speaks tests -- yikes).

— there better way test this? should test like? currently, unit tests associations , pass; section of feature test failing below.

  context 'creating posts'     scenario 'prompts user fill out form, displays new post'       user_sign_in        visit '/posts'       click_link 'add post'       fill_in 'caption', with: 'sunny saturday'       attach_file 'post_image', './public/img.jpg'       click_button 'create post'       expect(page).to have_content 'sunny saturday'       expect(current_path).to eq '/'     end   end 

which confirms i'm seeing locally.

failures:

  1) posts - creating posts prompts user fill out form, displays new post      failure/error: expect(page).to have_content 'sunny saturday'        expected find text "sunny saturday" in "sign out no posts yet add post" 

edited here development log details:

started post "/posts" ::1 @ 2015-07-14 13:28:20 +0200 processing postscontroller#create html   parameters: {"utf8"=>"✓", "authenticity_token"=>"mc4hur1kzf4p+esw09518zuwouhsldj5chiibi427thxti76pg0yvr+a7/kmhisgjmln+yugyddrnoxy4lvdrw==", "post"=>{"caption"=>"new post", "image"=>#<actiondispatch::http::uploadedfile:0x007fe121b00b30 @tempfile=#<tempfile:/var/folders/cc/nyvtq7hj62s98br08bcfzg6w0000gn/t/rackmultipart20150714-93392-arzgnw.jpg>, @original_filename="cat1.jpg", @content_type="image/jpeg", @headers="content-disposition: form-data; name=\"post[image]\"; filename=\"cat1.jpg\"\r\ncontent-type: image/jpeg\r\n">}, "commit"=>"create post"}   [1m[35muser load (0.3ms)[0m  select  "users".* "users" "users"."id" = $1  order "users"."id" asc limit 1  [["id", 1]] command :: path=c:\program files (x86)\gnuwin32\bin:$path; file -b --mime '/var/folders/cc/nyvtq7hj62s98br08bcfzg6w0000gn/t/a980d10665f268b0ec6c13ebea43034f20150714-93392-1qoui22.jpg' [paperclip] content type spoof: filename cat1.jpg (["image/jpeg"]), content type discovered file command: . see documentation allow combination.   [1m[36m (0.2ms)[0m  [1mbegin[0m command :: path=c:\program files (x86)\gnuwin32\bin:$path; file -b --mime '/var/folders/cc/nyvtq7hj62s98br08bcfzg6w0000gn/t/a980d10665f268b0ec6c13ebea43034f20150714-93392-7beczf.jpg' [paperclip] content type spoof: filename cat1.jpg (["image/jpeg"]), content type discovered file command: . see documentation allow combination.   [1m[35m (0.1ms)[0m  rollback redirected http://localhost:3000/ completed 302 found in 21ms (activerecord: 0.6ms)   started "/" ::1 @ 2015-07-14 13:28:20 +0200 processing postscontroller#index html   [1m[36m (0.2ms)[0m  [1mselect count(*) "posts"[0m   [1m[35mpost load (0.2ms)[0m  select "posts".* "posts"   rendered posts/index.html.erb within layouts/application (4.6ms)   [1m[36muser load (0.4ms)[0m  [1mselect  "users".* "users" "users"."id" = $1  order "users"."id" asc limit 1[0m  [["id", 1]] completed 200 ok in 28ms (views: 26.3ms | activerecord: 0.8ms)   started "/images/medium/missing.png" ::1 @ 2015-07-14 13:28:20 +0200  actioncontroller::routingerror (no route matches [get] "/images/medium/missing.png"): 

and yes, in future, refactor after things work.

what correct way pass in parameters methods ‘create' , ‘new’?

your form looks me.

is there else in code given here might preventing posts being saved db?

your create action doesn't save record.

 def create     @post = post.new(post_params)     @post.save     redirect_to '/'   end 

the errors see missing.png — i’m assuming because there no default image loaded, 1 called “missing.png”.

right


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -