database - Rails 4: new instance creation through form_for not working -


this follow-up question on rails 4: new instance created , saved database not displayed in view.

in our rails 4 app, there 4 models:

class user < activerecord::base   has_many :administrations, dependent: :destroy   has_many :calendars, through: :administrations end  class administration < activerecord::base   belongs_to :user   belongs_to :calendar end  class calendar < activerecord::base   has_many :administrations, dependent: :destroy   has_many :users, through: :administrations end  class post < activerecord::base     belongs_to :calendar end 

here corresponding migrations:

class createusers < activerecord::migration   def change     create_table :users |t|       t.string :first_name       t.string :last_name       t.string :email       t.integer :total_calendar_count       t.integer :owned_calendar_count        t.timestamps null: false     end   end end  class createadministrations < activerecord::migration   def change     create_table :administrations |t|       t.references :user, index: true, foreign_key: true       t.references :calendar, index: true, foreign_key: true       t.string :role        t.timestamps null: false     end   end end  class createcalendars < activerecord::migration   def change     create_table :calendars |t|       t.string :name        t.timestamps null: false     end   end end  class createposts < activerecord::migration   def change     create_table :posts |t|         t.references :calendar, index: true, foreign_key: true         t.date :date         t.time :time         t.string :focus         t.string :format         t.string :blog_title         t.text :long_copy         t.text :short_copy         t.string :link         t.string :hashtag         t.string :media         t.float :promotion         t.string :target         t.integer :approval         t.text :comment        t.timestamps null: false     end   end end 

we have following form create posts:

<h2>create new post</h2>  <%= form_for(@post) |f| %>    <%= render 'shared/error_messages', object: f.object %>      <tr>         <%= f.hidden_field :calendar_id, value: @calendar.id %>         <td class="field"><%= f.date_field :date, placeholder: "when want publish post?" %></td>         <td class="field"><%= f.time_field :time, placeholder: "what time want publish post?" %></td>         <td class="field"><%= f.text_field :focus, placeholder: "what post about?" %></td>         <td class="field"><%= f.text_field :format, placeholder: "what type of post this?" %></td>         <td class="field"><%= f.text_field :blog_title, placeholder: "if post blog post, title of blog post?" %></td>         <td class="field"><%= f.text_area :long_copy, placeholder: "what copy of post?" %></td>         <td class="field"><%= f.text_area :short_copy, placeholder: "what short copy of post (to used on twitter instance)?" %></td>         <td class="field"><%= f.url_field :link, placeholder: "which link want embed in post?" %></td>         <td class="field"><%= f.text_field :hashtag, placeholder: "which hashtag(s) want in post?" %></td>         <td class="field"><%= f.text_field :media, placeholder: "which media file (image, video) want include in post?" %></td>         <td class="field"><%= f.number_field :promotion, placeholder: "what advertising budget should allocated post?" %></td>         <td class="field"><%= f.text_field :target, placeholder: "who want target post?" %></td>         <td class="field"><%= f.select(:approval, %w['approved' 'needs edits' 'to deleted'], {prompt: 'how post look?'}) %></td>         <td class="field"><%= f.text_area :comment, placeholder: "any comment?" %></td>          <td><%= f.submit "create", class: "btn btn-primary" %></td>      </tr>  <% end %> 

this form embedded calendars#show view, post created, should appear in corresponding calendar.

here our postscontroller:

class postscontroller < applicationcontroller    def create     @post = post.create(post_params)     if @post.save       redirect_to root_url     else       render root_url     end   end  end 

currently, whenever submit form, nothing happens:

  • all values stay put in form
  • the page not reloaded (so, no error message appears)
  • no new @post added database

edit: here server logs:

started "/calendars/1" ::1 @ 2015-07-07 22:21:07 -0700 processing calendarscontroller#show html   parameters: {"id"=>"1"}   calendar load (0.1ms)  select  "calendars".* "calendars" "calendars"."id" = ? limit 1  [["id", 1]]   post exists (0.1ms)  select  1 one "posts" "posts"."calendar_id" = ? limit 1  [["calendar_id", 1]]   post load (0.1ms)  select "posts".* "posts" "posts"."calendar_id" = ?  [["calendar_id", 1]]   rendered posts/_post.html.erb (0.7ms)   rendered shared/_error_messages.html.erb (0.0ms)   rendered shared/_post_form.html.erb (5.1ms)   rendered calendars/show.html.erb within layouts/application (13.7ms)   rendered layouts/_shim.html.erb (0.1ms)   user load (0.1ms)  select  "users".* "users" "users"."id" = ? limit 1  [["id", 1]]   rendered layouts/_header.html.erb (0.9ms)   rendered layouts/_footer.html.erb (0.4ms) completed 200 ok in 145ms (views: 144.0ms | activerecord: 0.4ms) 

edit 2: here new server logs, after implementing @miler350 recommended in answer:

started post "/posts" ::1 @ 2015-07-08 09:09:58 -0700 processing postscontroller#create html   parameters: {"utf8"=>"✓", "authenticity_token"=>"+ls+b2/kiuq09cypzsqnzwj5xhbnnkyr6ec5hfjncwc3/nffkbwi3fje0zw3mumsdnubnip5piqhxz00ev1w2a==", "post"=>{"calendar_id"=>"", "date"=>"2015-08-20", "time"=>"12:04", "focus"=>"test", "format"=>"test", "blog_title"=>"test", "long_copy"=>"test", "short_copy"=>"test", "link"=>"http://www.google.com", "hashtag"=>"test", "media"=>"test", "promotion"=>"50", "target"=>"test", "approval"=>"'approved'", "comment"=>"test"}, "commit"=>"create"} unpermitted parameter: calendar_id    (0.1ms)  begin transaction   sql (0.7ms)  insert "posts" ("date", "time", "focus", "format", "blog_title", "long_copy", "short_copy", "link", "hashtag", "media", "promotion", "target", "approval", "comment", "created_at", "updated_at") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["date", "2015-08-20"], ["time", "2000-01-01 12:04:00.000000"], ["focus", "test"], ["format", "test"], ["blog_title", "test"], ["long_copy", "test"], ["short_copy", "test"], ["link", "http://www.google.com"], ["hashtag", "test"], ["media", "test"], ["promotion", 50.0], ["target", "test"], ["approval", 0], ["comment", "test"], ["created_at", "2015-07-08 16:09:58.605712"], ["updated_at", "2015-07-08 16:09:58.605712"]]    (0.6ms)  commit transaction    (0.0ms)  begin transaction    (0.0ms)  commit transaction redirected http://localhost:3000/ completed 302 found in 9ms (activerecord: 1.5ms)   started "/" ::1 @ 2015-07-08 09:09:58 -0700 processing staticpagescontroller#home html   user load (0.1ms)  select  "users".* "users" "users"."id" = ? limit 1  [["id", 1]]   rendered static_pages/home.html.erb within layouts/static_pages (1.8ms)   rendered layouts/_shim.html.erb (0.0ms)   rendered layouts/_static_header.html.erb (1.0ms)   rendered layouts/_footer.html.erb (0.3ms) completed 200 ok in 148ms (views: 147.5ms | activerecord: 0.1ms)   started "/users/1" ::1 @ 2015-07-08 09:10:02 -0700 processing userscontroller#show html   parameters: {"id"=>"1"}   user load (0.1ms)  select  "users".* "users" "users"."id" = ? limit 1  [["id", 1]]   user load (0.1ms)  select  "users".* "users" "users"."id" = ? limit 1  [["id", 1]]   rendered shared/_error_messages.html.erb (0.7ms)   rendered shared/_calendar_form.html.erb (5.0ms)   administration exists (0.5ms)  select  1 one "administrations" "administrations"."user_id" = ? limit 1  [["user_id", 1]]   administration load (0.5ms)  select "administrations".* "administrations" "administrations"."user_id" = ?  [["user_id", 1]]   calendar load (0.1ms)  select  "calendars".* "calendars" "calendars"."id" = ? limit 1  [["id", 1]]   calendar load (0.1ms)  select  "calendars".* "calendars" "calendars"."id" = ? limit 1  [["id", 2]]   calendar load (0.1ms)  select  "calendars".* "calendars" "calendars"."id" = ? limit 1  [["id", 3]]   calendar load (0.1ms)  select  "calendars".* "calendars" "calendars"."id" = ? limit 1  [["id", 4]]   calendar load (0.1ms)  select  "calendars".* "calendars" "calendars"."id" = ? limit 1  [["id", 5]]   rendered administrations/_administration.html.erb (4.1ms)   rendered users/show.html.erb within layouts/application (32.1ms)   rendered layouts/_shim.html.erb (0.3ms)   rendered layouts/_header.html.erb (0.9ms)   rendered layouts/_footer.html.erb (0.7ms) completed 200 ok in 150ms (views: 135.3ms | activerecord: 1.8ms)   started "/calendars/1" ::1 @ 2015-07-08 09:10:04 -0700 processing calendarscontroller#show html   parameters: {"id"=>"1"}   calendar load (0.1ms)  select  "calendars".* "calendars" "calendars"."id" = ? limit 1  [["id", 1]]   post exists (0.1ms)  select  1 one "posts" "posts"."calendar_id" = ? limit 1  [["calendar_id", 1]]   post load (0.1ms)  select "posts".* "posts" "posts"."calendar_id" = ?  [["calendar_id", 1]]   rendered posts/_post.html.erb (1.1ms)   rendered shared/_error_messages.html.erb (0.0ms)   rendered shared/_post_form.html.erb (5.3ms)   rendered calendars/show.html.erb within layouts/application (13.9ms)   rendered layouts/_shim.html.erb (0.1ms)   user load (0.1ms)  select  "users".* "users" "users"."id" = ? limit 1  [["id", 1]]   rendered layouts/_header.html.erb (0.8ms)   rendered layouts/_footer.html.erb (0.3ms) completed 200 ok in 113ms (views: 111.2ms | activerecord: 0.4ms) 

edit 3: here server logs when add :calendar_id permitted post_params in postscontrollers:

started "/calendars/1" ::1 @ 2015-07-08 11:07:39 -0700 processing calendarscontroller#show html   parameters: {"id"=>"1"}   calendar load (0.1ms)  select  "calendars".* "calendars" "calendars"."id" = ? limit 1  [["id", 1]]   post exists (0.2ms)  select  1 one "posts" "posts"."calendar_id" = ? limit 1  [["calendar_id", 1]]   post load (0.2ms)  select "posts".* "posts" "posts"."calendar_id" = ?  [["calendar_id", 1]]   rendered posts/_post.html.erb (0.7ms)   rendered shared/_error_messages.html.erb (0.0ms)   rendered shared/_post_form.html.erb (5.2ms)   rendered calendars/show.html.erb within layouts/application (17.9ms)   rendered layouts/_shim.html.erb (0.0ms)   user load (0.1ms)  select  "users".* "users" "users"."id" = ? limit 1  [["id", 1]]   rendered layouts/_header.html.erb (0.8ms)   rendered layouts/_footer.html.erb (0.3ms) completed 200 ok in 160ms (views: 147.4ms | activerecord: 0.8ms) 

where problem coming from: models, form, controller, or somewhere else?

you have create post params.

class postscontroller < applicationcontroller    def create     @post = post.create(post_params)     if @post.save       redirect_to root_url     else       render root_url     end   end    protected    def post_params     params.require(:post).permit(:date, :time, :focus....) #put columns want submit via form here.    end  end 

what getting validation error because require @ least 1 field submitted post. when rails doesn't receive this, tell render not redirect may not notice reload.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -