Started POST "/posts" for ::1 at 2016-01-16 20:55:15 -0800
Processing by PostsController#create as HTMLParameters: {"utf8"=>"✓", "authenticity_token"=>"RZREVs4Wy7ZWvo4u0xE5OmrawV2MYtxJNJZDfs+eQY8o2n1RthJsL5c1MklIpP+XlMGNbDYSD49fjna7Szq/1g==", "post"=>{"start"=>"2", "end"=>"1", "when(1i)"=>"2016", "when(2i)"=>"1", "when(3i)"=>"17", "when(4i)"=>"06", "when(5i)"=>"00"}}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.2ms)
ActiveRecord::AssociationTypeMismatch (Location(#70172044981120) expected, got String(#70171989781540)):app/controllers/posts_controller.rb:8:in `create'
Полная трассировка: ) lib/active_record/attribute_assignment.rb:54:in _assign_attribute'
activerecord (4.2.4) lib/active_record/attribute_assignment.rb:41:in
block в пакете действий assign_attributes (4.2.4) lib/action_controller/metal/strong_parameters.rb:185:in each_pair'
actionpack (4.2.4) lib/action_controller/metal/strong_parameters.rb:185:in
each_pair' activerecord (4.2.4) lib/active_record/attribute_assignment.rb :35:in assign_attributes'
activerecord (4.2.4) lib/active_record/core.rb:564:in
init_attributes' activerecord (4.2.4) lib/active_record/core.rb:281:in initialize'
activerecord (4.2.4) lib/active_record/inheritance.rb:61:in
new' activerecord (4.2.4) lib/active_record/inheritance.rb:61:in new'
activerecord (4.2.4) lib/active_record/persistence.rb:33:in
create' app/controllers/posts_controller. rb:8:в `создать'
пост.рб
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :start, :class_name => 'Location'
belongs_to :end, :class_name => 'Location'
validates :start, presence: true
validates :end, presence: true
validates :when, presence: true
validates :user_id, presence: true
end
PostsController.rb
class PostsController < ApplicationController
before_action :authenticate_user!
def new
@page_title = 'Add Post'
@post = Post.new
end
def create
@post = Post.create(post_params)
# save the post
if @post.save
flash[:notice] = "Post Created"
redirect_to posts_path
else
render 'new'
end
end
...
private
def post_params
params.require(:post).permit(:start, :end, :when, :description, :seats, :round_trip).merge(user_id: current_user.id)
end
новый.html.erb
<h2>Add New Post</h2>
<% if @post.errors.any? %>
<% @post.errors.full_messages.each do |msg| %>
<div class="alert alert-danger"><%= msg %> </div>
<% end %>
<% end %>
<%= form_for @post do |f| %>
<div class="form-group">
<%= f.select :start_location, Location.all.collect {|x [x.city_name, x.id]}, {:include_blank => 'Select One'} %>
</div>
<div class="form-group">
<%= f.select :end_location, Location.all.collect {|x| [x.city_name, x.id]}, {:include_blank => 'Select One'} %>
</div>
<div class="form-group">
<%= f.label :When %><br />
<%= f.datetime_select :when, :ampm => true, :minute_step => 15 %>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<%=link_to "Cancel", root_path, class:'btn btn-default' %>
<% end %>
схема.rb
ActiveRecord::Schema.define(version: 20160117041612) do
create_table "locations", force: :cascade do |t|
t.string "city_name"
t.string "state_name"
t.string "zip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "posts", force: :cascade do |t|
t.integer "start"
t.integer "end"
t.datetime "when"
t.text "description"
t.integer "seats"
t.integer "user_id"
t.integer "round_trip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "first_name", default: "", null: false
t.string "last_name", default: "", null: false
t.integer "location_id"
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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Я буквально понятия не имею, почему я получаю эту проблему. Пост имеет начальное и конечное местоположение. У него также есть user_id, где пользователь вошел в систему через devise. Пожалуйста помоги.
:end
во что-то другое (например,:end_location
)? - person taglia   schedule 17.01.2016