I am trying to create a form that will add a row to my table but I am getting this error and I am not sure why:
Started POST "/test" for ::1 at 2014-11-30 01:51:49 -0800
Processing by UsersController#create as JS
Parameters: {"utf8"=>"✓", "users"=>{"first_name"=>"brady", "last_name"=>"LIII", "email"=>"brad#gmail.com", "phone_number"=>"123456789"}, "commit"=>"Save Users"}
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/users_controller.rb:32:in `create'
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/rescues/_source.erb (2.0ms)
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/rescues/_trace.text.erb (0.9ms)
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/rescues/_request_and_response.text.erb (0.5ms)
Rendered /Users/bli1/.rvm/gems/ruby-2.1.3/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates/rescues/diagnostics.text.erb (11.5ms)
::1 - - [30/Nov/2014:01:51:49 -0800] "POST /test HTTP/1.1" 500 92618 0.0449
My form:
<!-- contact -->
<section id="contact">
<div class="container">
<div class="title-container">Contact Us</div>
<div class="title-caption">Reach us at (415)-911-9999</div>
<%= form_for(:users, remote: true, id: "contact-form", class: "contact-input") do |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>
<%= f.submit(class: "btn btn-xl") %>
<% end %>
</div>
</section>
My controller:
class UsersController < ApplicationController
def new
end
def create
if !(User.find_by(email: params[:user][:email]))
#user = User.new(user_params)
#user.save
end
end
private
def user_params
params.require(:users).permit(:first_name, :last_name, :email, :phone_number)
end
end
my user model
class User < ActiveRecord::Base
before_save { |user| user.email = user.email.downcase }
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
my routes
Rails.application.routes.draw do
root 'home#index'
get 'test', to: "users#new"
post 'test', to: "users#create"
schema:
ActiveRecord::Schema.define(version: 20141130075753) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "contact_requests", force: true do |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 do |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
Related
I'm attempting to add custom fields like first name, last name, avatar_url, and bio to my Spree user.
I added the fields to the table successfully:
class AddFieldsToSpreeUser < ActiveRecord::Migration[6.0]
def change
add_column :spree_users, :first_name, :string
add_column :spree_users, :last_name, :string
add_column :spree_users, :bio, :text
add_column :spree_users, :avatar_url, :string
end
end
I checked and the schema was updated according to plan:
create_table "spree_users", id: :serial, force: :cascade do |t|
t.string "encrypted_password", limit: 128
t.string "password_salt", limit: 128
t.string "email"
t.string "remember_token"
t.string "persistence_token"
t.string "reset_password_token"
t.string "perishable_token"
t.integer "sign_in_count", default: 0, null: false
t.integer "failed_attempts", default: 0, null: false
t.datetime "last_request_at"
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.string "login"
t.integer "ship_address_id"
t.integer "bill_address_id"
t.string "authentication_token"
t.string "unlock_token"
t.datetime "locked_at"
t.datetime "reset_password_sent_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "spree_api_key", limit: 48
t.datetime "remember_created_at"
t.datetime "deleted_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "first_name"
t.string "last_name"
t.text "bio"
t.string "avatar_url"
t.index ["bill_address_id"], name: "index_spree_users_on_bill_address_id"
t.index ["deleted_at"], name: "index_spree_users_on_deleted_at"
t.index ["email"], name: "email_idx_unique", unique: true
t.index ["ship_address_id"], name: "index_spree_users_on_ship_address_id"
t.index ["spree_api_key"], name: "index_spree_users_on_spree_api_key"
end
I added the new attributes to the Spree initializer:
Spree::PermittedAttributes.user_attributes << [:first_name]
Spree::PermittedAttributes.user_attributes << [:last_name]
Spree::PermittedAttributes.user_attributes << [:bio]
Spree::PermittedAttributes.user_attributes << [:avatar_url]
When I try to create a new user in rails c the new attributes come up as part of the model.
I have the following form to attempt to update the user:
<%= simple_form_for current_spree_user, url: spree.spree_user_registration_path(current_spree_user) do |f| %>
<%= f.object.errors.full_messages.join(", ") if f.object.errors.any? %>
<div class="row text-left">
<div class="col-sm-6 py-3">
<%= f.label "First Name" %>
<%= f.text_field :first_name, class: "form-control", required: true, autofocus: true, input_html: { autocomplete: "fname" } %>
</div> <!-- col -->
<div class="col-sm-6 py-3">
<%= f.label "Last Name" %>
<%= f.text_field :last_name, class: "form-control", input_html: { autocomplete: "lname" } %>
</div> <!-- col -->
<div class="col-12 py-3">
<%= f.label "Author Bio" %>
<%= f.text_area :bio, class: "form-control" %>
</div> <!-- col -->
<div class="col-12 py-3">
<%= f.label "Profile Image URL" %>
<%= f.text_field :avatar_url, class: "form-control", input_html: { autocomplete: "lname" } %>
</div> <!-- col -->
</div> <!-- row -->
<div class="form-actions text-center">
<%= f.button :submit, "Let's Go", class: "btn blue" %>
</div>
<% end %>
When I hit submit, I get a routing error:
ActionController::UnknownFormat
I have tried specifying method: :patch but it gets the same result. Can anyone see where I'm going wrong here?
I'm using the cocoon gem to build a form that creates a Tournament. A Tournament has_many Games. Cocoon lets me dynamically add more games to the form.
When I call #tournament.save, it generates the following errors:
Games team one must exist
Games team two must exist
tournament.rb
class Tournament < ApplicationRecord
has_many :games
accepts_nested_attributes_for :games, allow_destroy: true
end
game.rb
class Game < ApplicationRecord
belongs_to :tournament, optional: false
belongs_to :team_one, polymorphic: true
belongs_to :team_two, polymorphic: true
belongs_to :field, optional: true
end
schema.rb
ActiveRecord::Schema.define(version: 2019_12_24_011346) do
...
create_table "club_teams", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "fields", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "games", force: :cascade do |t|
t.bigint "tournament_id", null: false
t.string "team_one_type", null: false
t.bigint "team_one_id", null: false
t.string "team_two_type", null: false
t.bigint "team_two_id", null: false
t.bigint "field_id", null: false
t.date "date"
t.datetime "start_time"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["field_id"], name: "index_games_on_field_id"
t.index ["team_one_type", "team_one_id"], name: "index_games_on_team_one_type_and_team_one_id"
t.index ["team_two_type", "team_two_id"], name: "index_games_on_team_two_type_and_team_two_id"
t.index ["tournament_id"], name: "index_games_on_tournament_id"
end
create_table "high_school_teams", force: :cascade do |t|
t.string "school_name"
t.string "team_name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "tournaments", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
add_foreign_key "games", "fields"
add_foreign_key "games", "tournaments"
end
tournaments_controller.rb
class TournamentsController < ApplicationController
...
def create
#tournament = Tournament.new(tournament_params)
if #tournament.save
redirect_to #tournament
else
render 'new'
end
end
private
def tournament_params
params
.require(:tournament)
.permit(:name, games_attributes: [:id, :_destroy, :team_one_id, :team_two_id, :field_id, :date, :start_time])
end
end
request parameters in tournaments_controller#create
{
"authenticity_token"=>"iB4JefT9jRdiOFKok38OtjzMwd6Dv3hlHP/QZRtlFgMuVZfbn9PFD7Lebc1DuvfL6/IatDpS5CiubTci5MsCFg==",
"tournament"=>{
"name"=>"foo",
"games_attributes"=>{
"1577935885397"=>{
"team_one_id"=>"high-school-team-2",
"team_two_id"=>"club-team-2",
"date"=>"",
"start_time"=>"",
"_destroy"=>"false"
}
}
},
"commit"=>"Create Tournament"
}
tournament_params in tournaments_controller#create
<ActionController::Parameters {
"name"=>"foo",
"games_attributes"=><ActionController::Parameters {
"1577937916236"=><ActionController::Parameters {
"_destroy"=>"false",
"team_one_id"=>"high-school-team-2",
"team_two_id"=>"club-team-2",
"date"=>"",
"start_time"=>""
} permitted: true>
} permitted: true>
} permitted: true>
It seems to me that the tournament_params match what the accepts_nested_attributes documentation is expecting under One-to-many, so I don't see why there is an error.
Nested attributes for an associated collection can also be passed in
the form of a hash of hashes instead of an array of hashes:
Member.create(
name: 'joe',
posts_attributes: {
first: { title: 'Foo' },
second: { title: 'Bar' }
}
)
has the same effect as
Member.create(
name: 'joe',
posts_attributes: [
{ title: 'Foo' },
{ title: 'Bar' }
]
)
Edit:
tournaments/new.html.erb
<h1>Create a tournament</h1>
<%= render 'form' %>
<%= link_to 'Back', tournaments_path %>
tournaments/_form.html.erb
<%= form_with model: #tournament, class: 'tournament-form' do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<section class="games">
<%= f.fields_for :games do |game| %>
<%= render 'game_fields', f: game %>
<% end %>
<hr>
<p>
<%= link_to_add_association "Add game", f, :games,
data: {
association_insertion_node: '.games',
association_insertion_method: :prepend
}
%>
</p>
</section>
<p>
<%= f.submit %>
</p>
<% end %>
tournaments/_game_fields.html.erb
<section class="nested-fields">
<hr>
<p><strong>Game</strong></p>
<%= render "games/form_fields", f: f %>
<p><%= link_to_remove_association "Remove game", f %></p>
</section>
games/_form_fields.html.erb
<section>
<% if HighSchoolTeam.all.count + ClubTeam.all.count < 2 %>
<p>You neeed at least two teams to create a game. Create more high school and/or club teams first.</p>
<% else %>
<section class="game-form">
<p>
<%= f.label :team_one %><br>
<%= f.select :team_one_id, nil, {}, class: "team-one-dropdown" do %>
<optgroup label="High School Teams">
<% HighSchoolTeam.all.each do |high_school_team| %>
<option value="high-school-team-<%= high_school_team.id %>"><%= high_school_team.school_name %></option>
<% end %>
</optgroup>
<optgroup label="Club Teams">
<% ClubTeam.all.each do |club_team| %>
<option value="club-team-<%= club_team.id %>"><%= club_team.name %></option>
<% end %>
</optgroup>
<% end %>
</p>
<p>
<%= f.label :team_two %><br>
<%= f.select :team_two_id, nil, {}, class: "team-two-dropdown" do %>
<optgroup label="High School Teams">
<% HighSchoolTeam.all.each do |high_school_team| %>
<option value="high-school-team-<%= high_school_team.id %>"><%= high_school_team.school_name %></option>
<% end %>
</optgroup>
<optgroup label="Club Teams">
<% ClubTeam.all.each do |club_team| %>
<option value="club-team-<%= club_team.id %>"><%= club_team.name %></option>
<% end %>
</optgroup>
<% end %>
</p>
<p>
<%= f.label :field %><br>
<%= f.collection_select(:field_id, Field.all, :id, :name) %>
</p>
<p>
<%= f.label :date %><br>
<%= f.date_field :date %>
</p>
<p>
<%= f.label :start_time %><br>
<%= f.time_field :start_time %>
</p>
</section>
<% end %>
</section>
You seem to have problem saving team and not an issue of Cocoon gem.
Since you customize your select value to club-team-id and high-school-team-id. I think you just need to change it to something like this:
<option value="HighSchoolTeam-<%= high_school_team.id %>"><%= high_school_team.school_name %></option>
and
<option value="ClubTeam-<%= club_team.id %>"><%= club_team.name %></option>
Then the params will be
{
"authenticity_token"=>"iB4JefT9jRdiOFKok38OtjzMwd6Dv3hlHP/QZRtlFgMuVZfbn9PFD7Lebc1DuvfL6/IatDpS5CiubTci5MsCFg==",
"tournament"=>{
"name"=>"foo",
"games_attributes"=>{
"1577935885397"=>{
"team_one_id"=>"HighSchoolTeam-2",
"team_two_id"=>"ClubTeam-2",
"date"=>"",
"start_time"=>"",
"_destroy"=>"false"
}
}
},
"commit"=>"Create Tournament"
}
then you need to modified your params by:
# Adding before_action on top of your controller
before_action :modify_params, only: [:create, :update]
private
# Not the cleanest way, but this is what I can think of right now.
def modify_params
params.dig(:tournament, :games_attributes).each do |game_id, game_attribute|
team_one_type = game_attribute[:team_one_id].split('-').first
team_one_id = game_attribute[:team_one_id].split('-').last
team_two_type = game_attribute[:team_two_id].split('-').first
team_two_id = game_attribute[:team_two_id].split('-').last
params[:tournament][:games_attributes][game_id] = game_attribute.merge(
team_one_type: team_one_type,
team_one_id: team_one_id,
team_two_type: team_two_type,
team_two_id: team_two_id
)
end
end
# And update this method to allow team_one_type and team_two_type
def tournament_params
params.require(:tournament)
.permit(:name, games_attributes: [:id, :_destroy, :team_one_id, :team_two_id, :team_one_type, :team_two_type, :field_id, :date, :start_time])
end
I added an extra field in devise when a user signs up: "barge_name", when i go to the sign up page I enter a barge_name, email address and password this all works.
But when i try rails c, User.first is says barge_name is nil, I don't know why the value doesn't get picked up when signing up? Can someone help me with this?
#<User id: 1, email: "barcelona#gmail.com", created_at: "2019-10-31 17:43:21", updated_at: "2019-10-31 17:43:21", barge_name: nil>]
schema
create_table "users", force: :cascade do |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.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "barge_name"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
migration files
class AddDetailsToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :barge_name, :string
end
end
class DeviseCreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
end
end
form
<h2>Sign up</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :barge_name,
required: true,
autofocus: true,
input_html: { autocomplete: "bargename" }%>
<%= f.input :email,
required: true,
autofocus: true,
input_html: { autocomplete: "email" }%>
<%= f.input :password,
required: true,
hint: ("#{#minimum_password_length} characters minimum" if #minimum_password_length),
input_html: { autocomplete: "new-password" } %>
<%= f.input :password_confirmation,
required: true,
input_html: { autocomplete: "new-password" } %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
controller
class PositionsController < ApplicationController
def new
#position = Position.new
end
def index
#positions = Position.all
end
def show
#position = Position.find(params[:id])
end
def create
#position = Position.new(position_params)
if #position.save!
redirect_to #position
PositionMailer.general_message(#position).deliver
else
render :new
end
end
private def position_params
params.require(:position).permit(:date, :time, :activity, :tripnumber)
end
end
Started POST "/users" for ::1 at 2019-11-01 18:15:31 +0100
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"K1WdKPpHZKESmfr7tZqi7yj2qM1SMIZWpXconGMbaZMZYrU1VRrpD6plMu2i79m6tWWYdAHQM98sPJzv2bDi4w==", "user"=>{"barge_name"=>"test", "email"=>"test#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameter: :barge_name
You have to add the new param like this.
application_controller.rb
before_action :configure_permitted_parameters, if: :devise_controller?
...
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:barge_name])
devise_parameter_sanitizer.permit(:account_update, keys: [:barge_name])
end
Take notice of the two lines, one is for sign_up and one is for account_update, so new and edit actions basically.
I am trying to update my two tables through my form but I am getting this error:
Started POST "/test" for ::1 at 2014-12-02 00:15:21 -0800
Processing by UsersController#create as 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 for 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 and documentation and I think I wrote my form correctly but I think my controller is missing some aspects because I am new to rails.
Here is my schema:
ActiveRecord::Schema.define(version: 20141130075753) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "contact_requests", force: true do |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 do |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, which requires us to tell Rails exactly which parameters
# we want to 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 us at (415)-911-9999</div>
<%= form_for(:users, remote: true, id: "contact-form", class: "contact-input") do |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 do |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>
I have 3 tables called pins (posts), genres (categories) and genre_pin (many to many between pins and genres because pins can have many genres).
I am trying to build the create form, which will allow the user to select as many genres as they want on a pin and save / create. The create works just fine without error, but it is not saving any relationship records to the genre_pin table. I'm pretty sure I may be missing something very obvious here.
I am using the terms 'pins' and 'genres', but you can think of these as the same as 'posts' and 'categories'.
Pin Model
class Pin < ActiveRecord::Base
belongs_to :user
belongs_to :type
has_many :replies
has_many :genre_pins
has_many :genres, :through => :genre_pins
accepts_nested_attributes_for :genres, allow_destroy: true, reject_if: proc { |genre| genre[:id].blank? }
validates :user_id, presence: true
validates :type_id, presence: true
validates :title, presence: true
validates :description, presence: true
end
Genre Model
class Genre < ActiveRecord::Base
has_many :genre_pins
has_many :pins, :through => :genre_pins
validates :title, presence: true
end
GenrePin Model
class GenrePin < ActiveRecord::Base
belongs_to :pin
belongs_to :genre
validates_uniqueness_of :pin_id, :scope => :genre_id
end
Next, in my pin#new view, I have the following form:
Pin New View
<%= form_for :pin, url: pins_path do |f| %>
<div class="small-12 columns">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="small-12 columns">
<%= f.label :description %>
<%= f.text_area :description, :rows => 10 %>
</div>
<div class="small-12 columns">
<%= f.label :type %>
<%= f.collection_select :type_id, Type.order(:title), :id, :title, include_blank: false %>
</div>
<div class="small-12 columns">
<%= f.label :genres %>
<%= f.collection_select :genre_ids, Genre.order(:title), :id, :title, {}, { :multiple => true } %>
</div>
<div class="small-12 columns">
<%= f.submit "Create Pin", :class => "button" %>
</div>
<% end %>
The view works fine and gets me the available genres from the genres table. However, when I go to save the form, the record (pin) is created, but the relationship with the genres is not. What am I missing here? No errors are thrown, it's just that nothing happens. I am assuming Rails should be creating these PinGenre records through a pins association with genre.
Pin Controller (create)
def create
#pin = Pin.new(pin_params)
#pin.user_id = current_user.id
#pin.save
if #pin.save
flash[:success] = "Pin successfully created"
redirect_to #pin
else
flash[:warning] = #pin.errors.full_messages.to_sentence
redirect_to :action => "new"
end
end
private
def pin_params
params.require(:pin).permit(:title, :description, :type_id, :genre_ids)
end
Are there further steps I need to make here? Do the records need to be explicitly created by myself?
Thanks for your help and patience. I hope I've made this read clearly.
Michael.
Schema.rb
ActiveRecord::Schema.define(version: 0) do
create_table "genre_pins", force: true do |t|
t.integer "pin_id"
t.integer "genre_id"
end
create_table "genres", force: true do |t|
t.string "title", null: false
end
create_table "pins", force: true do |t|
t.integer "user_id", null: false
t.string "title", limit: 160, null: false
t.text "description", null: false
t.decimal "latitude", precision: 10, scale: 8
t.decimal "longitude", precision: 11, scale: 8
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "deleted_at"
t.integer "type_id", null: false
end
create_table "profiles", force: true do |t|
t.integer "user_id", null: false
t.string "first_name", null: false
t.string "last_name", null: false
t.string "gender"
t.string "email"
t.text "bio"
t.decimal "latitude", precision: 10, scale: 8
t.decimal "longitude", precision: 11, scale: 8
t.string "image", limit: 2000
t.datetime "updated_at"
t.datetime "created_at"
t.datetime "deleted_at"
end
create_table "replies", force: true do |t|
t.integer "pin_id", null: false
t.integer "user_id", null: false
t.text "reply", null: false
t.datetime "updated_at"
t.datetime "created_at"
t.datetime "deleted_at"
end
create_table "saves", force: true do |t|
t.integer "pin_id", null: false
t.integer "user_id", null: false
t.datetime "updated_at"
t.datetime "created_at"
t.datetime "deleted_at"
end
create_table "settings", force: true do |t|
t.integer "user_id", null: false
t.boolean "show_email", null: false
t.boolean "show_telephone", null: false
t.integer "timezone", null: false
t.datetime "updated_at"
t.datetime "created_at"
t.datetime "deleted_at"
end
create_table "types", force: true do |t|
t.string "title", null: false
end
create_table "users", force: true do |t|
t.string "provider", limit: 45, null: false
t.string "provider_id", limit: 45, null: false
t.string "oauth_token", limit: 1000
t.datetime "oauth_expires_at"
t.datetime "updated_at"
t.datetime "created_at"
t.datetime "deleted_at"
end
end
Generated View Source
<form accept-charset="UTF-8" action="/pins" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="3VO3kVR/62JFf6dJs+yyPTPvYJb3fsUSLRFljSE0Jdk=" /></div>
<div class="small-12 columns">
<label for="pin_title">Title</label>
<input id="pin_title" name="pin[title]" type="text" />
</div>
<div class="small-12 columns">
<label for="pin_description">Description</label>
<textarea id="pin_description" name="pin[description]" rows="10">
</textarea>
</div>
<div class="small-12 columns">
<label for="pin_type">Type</label>
<select id="pin_type_id" name="pin[type_id]"><option value="2">Available</option>
<option value="1">Wanted</option></select>
</div>
<div class="small-12 columns">
<label for="pin_genres">Genres</label>
<input name="pin[genre_ids][]" type="hidden" value="" /><select id="pin_genre_ids" multiple="multiple" name="pin[genre_ids][]"><option value="5">Ambient</option>
<option value="4">Electronic</option>
<option value="2">Indie</option>
<option value="3">Jazz</option>
<option value="6">Metal</option>
<option value="1">Rock</option></select>
</div>
<div class="small-12 columns">
<input class="button" name="commit" type="submit" value="Create Pin" />
</div>
</form>
You need to tell your controller that 'genre_ids` is expected to be an array:
params.require(:pin).permit(:title, :description, :type_id, genre_ids: [])
However I expect you will get another error (unknown attribute gener_ids). Let me know if that happen.