ruby on rails - Updating two tables with one to many relationship via form -
i trying update 2 tables through form getting error:
started post "/test" ::1 @ 2014-12-02 00:15:21 -0800 processing userscontroller#create js parameters: {"utf8"=>"✓", "users"=>{"first_name"=>"sdfdsfdsf", "last_name"=>"dsfdsfds", "email"=>"3213213@hotmail.com", "phone_number"=>"23123213", "message"=>"sdfdsfsdfdsfkjsdfksdfk;adklsfjksadfksjdfklsdf"}, "commit"=>"save users"} completed 500 internal server error in 1ms argumenterror (wrong number of arguments (1 0)): app/controllers/users_controller.rb:32:in `create' rendered /users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_source.erb (2.6ms) rendered /users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_trace.text.erb (0.4ms) rendered /users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_request_and_response.text.erb (0.6ms) rendered /users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/diagnostics.text.erb (12.2ms)
i have been reading examples , documentation , think wrote form correctly think controller missing aspects because new rails.
here schema:
activerecord::schema.define(version: 20141130075753) # these extensions must enabled in order support database enable_extension "plpgsql" create_table "contact_requests", force: true |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "message", null: false t.integer "user_id" end add_index "contact_requests", ["user_id"], name: "index_contact_requests_on_user_id", using: :btree create_table "users", force: true |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "first_name", null: false t.string "last_name", null: false t.string "email", null: false t.string "phone_number" end end
models:
class user < activerecord::base has_many :contact_requests validates(:first_name, presence: true) validates(:last_name, presence: true) validates(:email, presence: true) accepts_nested_attributes_for :contact_requests end class contactrequest < activerecord::base belongs_to :user validates :user_id, presence: true validates :message, presence: true, length: { maximum: 500 } end
user controller:
def create if !(user.find_by(email: params[:users][:email])) @user = user.new(user_params) @contact_request = contactrequest.new(contact_request_params) @contact_request.save @user.save else @contact_request = contactrequest.new(contact_request_params) @contact_request.save end end private def user_params # strong_parameters, requires tell rails parameters # want accept in our controllers params.require(:users).permit(:first_name, :last_name, :email, :phone_number) end end
form:
<!-- contact --> <section id="contact"> <div class="container"> <div class="title-container">contact us</div> <div class="title-caption">reach @ (415)-911-9999</div> <%= form_for(:users, remote: true, id: "contact-form", class: "contact-input") |f| %> <div class="col-md-12"> <div class="col-md-6"> <div class="contact-input-margin form-group"> <%= f.text_field(:first_name, class: "form-control", placeholder: "first name")%> </div> <div class="contact-input-margin form-group"> <%= f.text_field(:last_name, class: "form-control", placeholder: "last name") %> </div> <div class="contact-input-margin form-group"> <%= f.email_field(:email, class: "form-control", placeholder: "email") %> </div> <div class="contact-input-margin form-group"> <%= f.telephone_field(:phone_number, class: "form-control", placeholder: "phone number") %> </div> </div> <div class="contact-input-margin col-md-6"> <div class="form-group"> <%= f.fields_for :contact_requests |builder| %> <%= f.text_area(:message, class: "form-control contact-margin", rows: "8", placeholder: "message...") %> <% end %> </div> </div> </div> <%= f.submit(class: "btn btn-xl") %> <% end %> </div> </section>
Comments
Post a Comment