Rails Acts As Messageable setting up Form - ruby-on-rails

I'm trying to use acts as message able gem and I'm following their example controller
SOLVED See Answer
I keep getting this error undefined method `send_message' for nil:NilClass when trying to send a message in the view
How should I adjust my code?
Thanks
View (Form)
<%= simple_form_for ActsAsMessageable::Message.new, :url => messages_path, :method => :post do |f| %>
<%= f.hidden_field :to, value: #gear.user.email %>
<%= f.input :body %>
<%= f.input :topic %>
<%= f.button :submit, class: 'btn' %>
<% end %>
User Model
class User < ActiveRecord::Base
acts_as_messageable :table_name => "messages", # default 'messages'
:required => [:topic, :body], # default [:topic, :body]
:class_name => "ActsAsMessageable::Message", # default "ActsAsMessageable::Message",
:dependent => :nullify # default :nullify
end
Messages Controller
class MessagesController < ApplicationController
def new
#message = ActsAsMessageable::Message.new
end
def create
#to = User.find_by_email(params[:acts_as_messageable_message][:to])
current_user.send_message(#to, params[:acts_as_messageable_message][:topic], params[:acts_as_messageable_message][:body])
end
end
Development Log
Started POST "/messages" for 127.0.0.1 at 2012-11-15 07:23:40 -0600
Processing by MessagesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"OqaDOP6PldbFVXWPZyijn+887Ym/fDsU0oqzVrL0rQA=", "acts_as_messageable_message"=>{"to"=>"xyz#test.com", "body"=>"test", "topic"=>"test"}, "commit"=>"Create Message"}
[1m[35mUser Load (0.5ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`email` = 'xyz#test.com' LIMIT 1
Completed 500 Internal Server Error in 3ms
NoMethodError (undefined method `send_message' for nil:NilClass):
app/controllers/messages_controller.rb:29:in `create
'

From the error message and provided code seems like your params obj is not formated like you think. Give params[:acts_as_messageable_message] a try instead of [:message]. If that doesn't work check the log to see what is being passed in as params.

I ended up getting it working. The problem ultimately was the gem was using the User model and was expecting the controller to be the User controller not another controller called "Messages". So I simply moved my actions into my Users controller, added the routes and changed the view path, and it now works. #Alex.Bullard thanks for the help.
I'm posting my edits below:
Controller Change
class UsersController < ApplicationController
respond_to :html, :json
def new_message
#message = ActsAsMessageable::Message.new
end
def create_message
#to = User.find_by_email(params[:acts_as_messageable_message][:to])
current_user.send_message(#to, params[:acts_as_messageable_message][:topic], params[:acts_as_messageable_message][:body])
redirect_to :back, notice: "Message sent to Owner"
end
end
View
<%= simple_form_for ActsAsMessageable::Message.new, :url => create_message_users_path, :method => :post do |f| %>
<%= f.hidden_field :to, value: #gear.user.email %>
<%= f.input :body %>
<%= f.input :topic %>
<%= f.button :submit, class: 'btn' %>
<% end %>
Routes
resources :users, :except => [ :create, :new ] do
get "new_message", :on => :collection
post "create_message", :on => :collection
resources :store
end

Related

How can I grab or access different model attribute when I send email using Sendgrid Rails Heroku

I am using devise, and scaffolded Textbook.
I like to implement my strategy.
When buyer clicks an #textbook.title -> Buyer can send an email to the #textbook's seller.
I have every model has column for 'user_email'
So, Whenever a seller create a #textbook, automatically current_user.email is saved into #textbook.user_email.
I just don't know how to grab the seller's user_email and send email.
I have following
Textbook model:
class Textbook < ActiveRecord::Base
belongs_to :user
validates :title, :presence => true
validates :subject, :presence => true
validates :price, :presence => true
validates :offer, :presence => false
validates :created_at, :presence => false
validates :user_email, :presence => true
validates :description, :presence => true
end
I am not sure this model syntax is right for subject and current_user.email
Contact model:
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :current_user.email, :validate => /\A([\w\.%\+\-]+)#([\w\-]+\.)+([\w]{2,})\z/i
attribute :message, :validate => true
def headers
{
:subject => "I like to buy #{#textbook.id.title}",
:to => "#textbook.id.user_email",
:from => %(<#{email}>)
}
end
end
My detail question is this:
If a user clicks 'contact' when buyer was inside of a specific textbook ant it links the user to textbook#show. Below is the form when the user clicked 'contact'.
How can I make sure this below view access the correct textbook.id or textbook.title?
<h1> Contact to the Seller </h1>
<div>
<%=form_for #contact do |f|%>
<h3>Send email for: <%=#textbook.id.title%> </h3>
<%= f.label :message %><br>
<%= f.text_area :message, as: :text %><br>
<%=f.submit 'Send message', class: 'button' %>
<%end%>
</div>
Specially, I don't know how to handle grab attributes that is from different model inside different views.
Thank you in advance!
-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!
Update 1:
I have contact controller like this:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(params[:contact])
##contact.request = request
if #contact.deliver
flash[:success] = "Email sent."
else
flash[:alert] = "Cannot send an email."
render :new
end
end
end
I just edited my 'class Contact < MailForm::Base'
class Contact < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)#([\w\-]+\.)+([\w]{2,})\z/i
attribute :message, :validate => true
def headers
{
:subject => "I like to buy #{textbook.title}",
:to => "#textbook.user_email",
:from => %(<#{current_user.email}>)
}
end
end
But I got error:
NameError in ContactsController#create
undefined local variable or method `textbook' for #<Contact:0x007fbac641be40>
Extracted source (around line #8):
def headers
{
:subject => "I like to buy #{textbook.title}",
:to => "#textbook.user_email",
:from => %(<#{current_user.email}>)
}
#zeiv I fixed textbook.title -> #textbook.title
I get error an another error.
NoMethodError in ContactsController#create
undefined method `title' for nil:NilClass
def headers
{
:subject => "I like to buy #{#textbook.title}",
:to => "#textbook.user_email",
:from => %(<#{current_user.email}>)
}
I have views/textbooks.html.erb:
<div class="container">
<p>
<h3><strong>Title:</strong>
<%= #textbook.title %></h3>
</p>
<p>
<strong>Subject:</strong>
<%= #textbook.subject %>
</p>
<p>
<strong>Price:</strong>
$<%= #textbook.price %>
</p>
<p>
<strong>Accept Offer:</strong>
<%if #textbook.offer == true%>
<%='Yes'%>
<%else%>
<%='No'%>
<%end%>
</p>
<p>
<strong>Description:</strong>
<pre><%= #textbook.description %></pre>
</p>
<p>
<strong>Image:</strong>
<pre><%= image_tag #textbook.thumbnail.url(:medium) %></pre>
</p>
<p>
<strong>Created on:</strong>
<%= #textbook.created_at.strftime("%d %b. %Y") %>
</p>
<p>
<%= link_to 'Contact', new_contact_path %>
</p>
<%if #textbook.user_email == current_user.email %>
<%= link_to 'Edit', edit_textbook_path(#textbook) %> |
<%= link_to 'Back to list', textbooks_path %>
<%else %>
<%= link_to 'Back to list', textbooks_path %>
<%end%>
And I have textbooks_controller:
class TextbooksController < ApplicationController
before_action :set_textbook, only: [:show, :edit, :update, :destroy]
#before_action :set_textbook, only: [:show]
#before_action :authorize_resource!, except: [:new, :index, :show]
# GET /textbooks
# GET /textbooks.json
def index
##textbooks = Textbook.all
#textbooks = Textbook.all.order(created_at: :desc).paginate(page: params[:page], per_page: 10)
##textbooks = Textbook.paginate(:page => params[:page], :per_page => 10)
end
# GET /textbooks/1
# GET /textbooks/1.json
def show
end
I have config/routes:
resources :textbooks
resources :contacts, only: [:new, :create]
devise_for :users
When I rake routes at this moment 4/17 5:05pm
new_textbook GET /textbooks/new(.:format) textbooks#new
edit_textbook GET /textbooks/:id/edit(.:format) textbooks#edit
textbook GET /textbooks/:id(.:format) textbooks#show
PATCH /textbooks/:id(.:format) textbooks#update
PUT /textbooks/:id(.:format) textbooks#update
DELETE /textbooks/:id(.:format) textbooks#destroy
contacts POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
UPDATE 2 -!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!#-!
below is after 04/17/2016 11:00pm
#zeiv I did what you told me.
But still I get error when I click 'contact' button in views/textbooks/show.html.erb
#views/textbooks/show.html.erb
<p>
<%= link_to 'Contact', new_contact_textbook_path %>
</p>
my routes.rb has now:
Rails.application.routes.draw do
resources :textbooks do
member do
get 'contact', to: 'textbooks#new_contact', as: 'new_contact'
post 'contact', to: 'textbooks#send_contact', as: 'send_contact'
end
end
rake routes has now:
Prefix Verb URI Pattern Controller#Action
new_contact_textbook GET /textbooks/:id/contact(.:format) textbooks#new_contact
send_contact_textbook POST /textbooks/:id/contact(.:format) textbooks#send_contact
textbooks GET /textbooks(.:format) textbooks#index
POST /textbooks(.:format) textbooks#create
new_textbook GET /textbooks/new(.:format) textbooks#new
edit_textbook GET /textbooks/:id/edit(.:format) textbooks#edit
textbook GET /textbooks/:id(.:format) textbooks#show
PATCH /textbooks/:id(.:format) textbooks#update
PUT /textbooks/:id(.:format) textbooks#update
DELETE /textbooks/:id(.:format) textbooks#destroy
The error I get is this:
NoMethodError in Textbooks#new_contact
undefined method `id' for nil:NilClass
Extracted source (around line #4):
<div>
Texbook id is: <%= #textbook.id %>
</div>
I am running heroku local the error shows:
10:56:13 PM web.1 | Rendered textbooks/new_contact.html.erb within layouts/application (2.0ms)
10:56:13 PM web.1 | Completed 500 Internal Server Error in 7ms (ActiveRecord: 0.1ms)
10:56:13 PM web.1 | ActionView::Template::Error (undefined method `id' for nil:NilClass):
10:56:13 PM web.1 | 1: <h1> contact seller! - working? </h1>
10:56:13 PM web.1 | 2:
10:56:13 PM web.1 | 3: <div>
10:56:13 PM web.1 | 4: Texbook id is: <%= #textbook.id %>
10:56:13 PM web.1 | 5: </div>
Basically what you need to do is to write your mailers and controllers in such a way that all the information you want is passed to the mailer. So if you want an instance of your Textbook model to be passed to the mailer, you will need to do so from the controller in which you send your email. You might event want to nest your contact controller routes within your textbook routes to help you. Alternatively, rather than having an entire controller for Contact, just have a contact action within your textbook controller.
# route.rb
...
resources :textbooks do
member do
get "contact", to: "textbooks#new_contact", as: "new_contact"
post "contact", to: "textbooks#send_contact", as: "send_contact"
end
end
That will give you routes like /textbook/24/contact. member do means that the routes are for individual instances of your model rather than the whole collection, so you will need to specify which textbook you are referring to when calling their helpers: new_contact_textbook_path(#textbook.id).
So in your Textbook controller, you would do this:
# textbooks_controller.rb
before_action :set_textbook, only: [:show, :edit, :update, :destroy, :new_contact, :send_contact]
...
def new_contact
# We are NOT doing Contact.new here
# Only put logic here that you need to display the form
end
def send_contact
message = params[:message]
if Contact.send_contact(#textbook, current_user, message).deliver
flash[:success] = "Email sent."
redirect_to #textbook
else
flash[:alert] = "There was a problem sending the email."
render :new_contact
end
end
Then put your new_contact.html.erb file in with your other Textbook views.
<h1> Contact to the Seller </h1>
<div>
<%= form_tag send_contact_textbook_path(#textbook.id) do %>
<h3>Send email for: <%=#textbook.title%> </h3>
<%= label_tag :message, "Type your message:" %><br>
<%= text_area_tag :message %><br>
<%= submit_tag 'Send message', class: 'button' %>
<%end%>
</div>
Notice that I'm using form_tag instead of form_for because we don't have a Contact object to pass it. (That is, Contact isn't a model. It's a mailer.)
Your mailer would then look something like this:
class Contact < ApplicationMailer
def send_contact(textbook, current_user, message)
#textbook = textbook
#current_user = current_user
#message = message
mail(
from: "#{#current_user.name} <#{#current_user.email}>",
to: #textbook.user.email,
subject: "I would like to buy #{#textbook.title}",
reply_to: #current_user.email,
)
end
end
And finally, put the template/view for you mailer in /app/views/contact/send_contact.html.erb:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1><%= #current_user.name %> is wondering about <%= #textbook.title %>:</h1>
<p><%= #message %></p>
</body>
</html>
And that should do it! Although you may have to adjust some things to suit your needs. Also see these links for more examples:
Contact Form Mailer in Rails 4
https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html

Undefined method error on User child object Rails 4

I have an object(Transaction) that belongs_to User and User has_many transactions. When I try and create this object in my rails form I get the following error:
undefined method `transaction_kind' for nil:NilClass
app/models/transaction.rb:10:in `create_transaction'
app/controllers/transactions_controller.rb:17:in `create'
The params hash being passed through my console after I submit the form looks like:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXXXX", "transaction"=>{"transaction_kind"=>"Deposit", "user_id"=>"113", "credit"=>"99"}, "commit"=>"Submit Request", "lender_id"=>"113"}
Transaction.rb looks like this:
class Transaction < ActiveRecord::Base
belongs_to :user
after_save :create_transaction
attr_accessible :transaction_kind, :user_id, :credit, :debit, :created_at
def create_transaction
client = Restforce.new
credit = '012c00000004k5A'
debit = '012c00000004k55'
if #transaction.transaction_kind == "Deposit"
client.create!('Transaction__c', Account__c: self.salesforce_id, RecordTypeId: credit, Debit_Amount__c: self.debit, Credit_Amount__c: self.credit, Recorded_On__c: self.created_at, Status__c: 'New Transaction', Type: 'Deposit', Transaction_Type__c: self.transaction_kind)
else
client.create!('Transaction__c', Account__c: self.salesforce_id, RecordTypeId: debit, Debit_Amount__c: self.debit, Credit_Amount__c: self.credit, Recorded_On__c: self.created_at, Status__c: 'New Transaction', Type: 'Withdrawal', Transaction_Type__c: self.transaction_kind)
end
end
end
new.html.erb has the following form code:
<%= form_for [#user, #transaction], url: lender_transaction_path(#user) do |f| %>
<%= f.hidden_field :transaction_kind, :value => "Deposit" %>
<%= f.hidden_field :user_id, :value => #user.id %>
<%= f.label :credit, :class => "required" %>
<%= f.text_field :credit, :autofocus => :true, :class => "form-control margin-bottom-20 required"%>
<%= f.submit 'Submit Request', :class => "btn-u btn-u-primary" %>
<% end %>
transactions_controller.rb
class TransactionsController < ApplicationController
before_filter :authenticate_user!
def new
#user = current_user
#transaction = Transaction.new
if #user.activated?
client = Restforce.new
#account = client.find('Account', #user.salesforce_id, 'Account_Id')
transaction_query = "select from where Account__c ='%s' AND " % #account.Id.to_s
#transactions = client.query(transaction_query)
end
end
def create
#user = current_user
#transaction = Transaction.create(transaction_params)
if #transaction.save
redirect_to new_lender_transaction_path(#user)
end
end
private
def transaction_params
params.require(:transaction).permit(:transaction_kind, :credit, :debit, :created_at)
end
end
Any help with this issue would be great. Or any ideas on how to debug this further. Thanks!
You can't pass an instance variable from controller to model, so the Transaction model doesn't know what #transaction is and thus supposes it is as a nil
I would change the code into this:
if self.transaction_kind == "Deposit"
The self will make it work. If it is not, try using attr_accessor :transaction_kind or simply put if transaction_kind == "Deposit"
Also, I believe it would be better if you refactor the client.create! into:
Restforce.create!(...)
which will save you from writing another client = Restforce.new line.

Rails4 and Cocoon - How to Handle Has_One Relationship

I'm trying to deal with nested forms in my Rails4 application but I'm having problems with has_one relationship because all the GEM documentation is only for has_many. When I try to create a new examination the app is throwing "Unpermitted parameters: exam_statuses" message. Probably I'm doing something wrong with singular&plural names but can you please check what is wrong with this code. Here is my codes;
examination.rb:
class Examination < ActiveRecord::Base
has_one :exam_status, dependent: :destroy
accepts_nested_attributes_for :exam_status, :reject_if => :all_blank, :allow_destroy => true
end
examination_controller.rb:
.....
def new
#examination = Examination.new
end
def create
#examination = Examination.new(examination_params)
respond_to do |format|
if #examination.save
format.html { redirect_to examinations_path, notice: 'success' }
else
format.html { render action: 'new' }
end
end
end
private
def set_examination
#examination = Examination.find(params[:id])
end
def examination_params
params.require(:examination).permit(:name, :shortname, :fee, :exam_status => [:first_application_date, :last_application_date, :examination_id])
end
views/examinations/new.html.erb =>
<%= simple_form_for(#examination) do |f| %>
<%= f.input :name %>
<%= f.input :shortname %>
<%= f.input :fee %>
<%= f.simple_fields_for :exam_status do |exam_status| %>
<%= render 'exam_status_fields', :f => exam_status %>
<% end %>
<% end %>
views/examinations/_exam_status_fields.html.erb =>
<div class="nested-fields">
<%= f.input :first_application_date, as: :datetime %>
<%= f.input :last_application_date, as: :datetime %>
<%= f.input :examination_id %>
</div>
Log File =>
Started POST "/examinations" for 127.0.0.1 at 2014-07-03 16:43:30 +0300
Processing by ExaminationsController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"uYpiZ9Z1txaJ1mib+NEAG7Ckwm9F3TyNQ6jRpYz3ncA=",
"examination"=>{"name"=>"nnnnn", "shortname"=>"nnnnnn", "fee"=>"33",
"exam_statuses"=>{"first_application_date(3i)"=>"3", "first_application_date(2i)"=>"7", "first_application_date(1i)"=>"2014", "first_application_date(4i)"=>"16", "first_application_date(5i)"=>"43", "last_application_date(3i)"=>"5", "last_application_date(2i)"=>"7", "last_application_date(1i)"=>"2014", "last_application_date(4i)"=>"16", "last_application_date(5i)"=>"43", "examination_id"=>""}}}
Unpermitted parameters: exam_statuses
You have a few issues here.
1. Controller
In your new method you need to build exam_status for examination:
# GET /examinations/new
def new
#examination = Examination.new
#examination.build_exam_status
end
And in your examination_params method, you need to change it to:
def examination_params
params.require(:examination).permit(:name, :shortname, :fee, exam_status_attributes: [:first_application_date, :last_application_date, :_destroy])
end
Notice that :exam_status needs to be exam_status_attributes and it doesn't need examination_id param (Rails takes care of that). Also note that I added _destroy. This is a flag used to destroy the object.
2. Nested fields partial
Remove examination_id field from the partial.
<div class="nested-fields">
<%= f.input :first_application_date, as: :datetime %>
<%= f.input :last_application_date, as: :datetime %>
</div>
These changes will take care of the issue.

Redirecting from polymorphic association

I have a comments model that belongs to two models: submissions and posts
class Comment < ActiveRecord::Base
attr_accessible :content, :show
belongs_to :commentable, :polymorphic => true
end
class Submission < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
Submissions is a nested route and post is not.
In my comments controller:
def create
#commentable = find_commentable
#comment = #commentable.comments.build(params[:comment])
#comment.user = current_user
if #comment.save
#CommentMailer.comment_email(#user, #comment, #commentable).deliver
flash[:notice] = "Successfully created comment."
if #commentable == #submission
redirect_to [#contest, #commentable]
else
redirect_to [#commentable]
end
else
render :action => 'new'
end
end
find_contest
def find_contest
#contest = Contest.find(params[:contest_id])
end
find_commentable:
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
The redirect to post via #commentable works fine, but the redirect to submissions is not finding the contest.
Started POST "/submissions/36/comments" for 127.0.0.1 at 2012-11-30 18:34:41 -0800
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"R62NH5/EE34FPapEqy7mfpa0wKz18GtSdhH8MGYq2Ec=", "comment"=>{"content"=>"test", "show"=>"true"}, "commit"=>"Create Comment", "submission_id"=>"36"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY users.created_at DESC LIMIT 1
Submission Load (0.3ms) SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 ORDER BY submissions.created_at DESC LIMIT 1 [["id", "36"]]
Completed 500 Internal Server Error in 116ms
ActiveRecord::RecordNotFound (Couldn't find Contest without an ID):
app/controllers/comments_controller.rb:19:in `create'
Change to submission routes:
submissions GET /submissions(.:format) submissions#index
POST /submissions(.:format) submissions#create
new_submission GET /submissions/new(.:format) submissions#new
edit_submission GET /submissions/:id/edit(.:format) submissions#edit
submission GET /submissions/:id(.:format) submissions#show
PUT /submissions/:id(.:format) submissions#update
DELETE /submissions/:id(.:format) submissions#destroy
Submission form:
<%= simple_form_for #submission, :html => { :multipart => true } do |f| %>
<div class="span7 offset2 submission">
<fieldset class="well pleft80 edit">
<%= f.hidden_field :contest_id , :value => params[:contest_id] %>
<%= f.input :title %>
<%= f.input :description %>
<%= f.input :comment_show, :as => :hidden, :input_html => { :value => true } %>
</fieldset>
<fieldset class="well pleft80 noborder">
<%= f.fields_for :image do |img_field| %>
<h3>Upload Photo<%= img_field.file_field :source %></h3>
<% end %>
</fieldset>
<div class ="form-actions pleft80">
<%= f.submit nil, :class => 'btn btn-primary btn-large' %>
</div>
</div>
<% end %>
You don't need to instantiate or classify anything.
redirect_to #comment.commentable
If you can't do that then you will need to build a global helper module for it and include that into the controller.
module RouteHelpers
def comment_association_redirect_to(comment)
item = comment.commentable
case item.class.to_s
when 'Submission'
redirect_to submission_path(item)
end
end
end
And include it within the ApplicationController:
include RouteHelpers
Then you can call comment_association_redirect_to anywhere in your app (controllers and so on).
I stripped the nested routing out of the app and now it works fine and it's much simpler. Not sure I can think of a good reason to use nested routing when the views must relate the dependencies.

Formtastic / rails forms not submitting

I am trying to use formtastic to make a form where I can enter an :opposition choose a :venue and a :team and then be presented with a list of players that I am able to check off to select them for the
I have got the form set up so it renders correctly however when submitted it does not save any information and just reloads the page.
My code is at my github here: https://github.com/jpknegtel/st_francis
models
This concerns the following models:
player
has_many :player_fixtures
has_many :fixtures, :through => :player_fixtures
fixtures
has_many :player_fixtures
has_many :players, :through => :player_fixtures
PlayerFixture
belongs_to :player
belongs_to :fixture
controller
def create
#fixture = Fixture.new(params[:fixture])
if #fixture.save
flash[:notice] = "Fixture Created"
redirect_to(:action =>'list')
else
flash.now[:error] = "Could not save fixture. Please re-enter information"
render('new')
end
end
def new
#fixture = Fixture.new
end
form
<%= semantic_form_for :fixture do |f| %>
<%= f.inputs do %>
<%= f.input :opposition %>
<%= f.input :team, :as => :select, :collection => Team.all %>
<%= f.input :venue, :as => :check_boxes, :collection => Hash[Venue.all.map{|b| [b.name, b.id]}]%>
<%= f.input :players, :as => :check_boxes, :collection => Hash[Player.all.map{|b| [b.full_name, b.id]}], :required => true %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :button %>
<%= f.action :cancel, :as => :link %>
<% end %>
<% end %>
So when the form is submitted now nothing is passed. When looking at the web brick server nothing gets submitted but the page just gets reloaded.
It is possible to insert the records using rails console.
EDIT: I can now see this when submitted.
Started POST "/fixtures/new" for 127.0.0.1 at 2012-04-23 15:00:21 +0100
Processing by FixturesController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Hx4TChWiUdhpZbAfgWUYMWBKao86pZh0tGzwVKy+P80=", "fixture"=> {"opposition"=>"Mid sussex", "team"=>"1", "venue"=>["", "1"], "players"=>["", "1", "3"]}, "button"=>""}
Team Load (1.0ms) SELECT `teams`.* FROM `teams`
Venue Load (1.0ms) SELECT `venues`.* FROM `venues`
Player Load (1.0ms) SELECT `players`.* FROM `players`
Rendered fixtures/new.html.erb within layouts/application (173.0ms)
Completed 200 OK in 200ms (Views: 163.0ms | ActiveRecord: 36.0ms)
[2012-04-23 15:00:21] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
My guess is massassignment. You need to allow rails to update some attributes via massassignment.
Add this line to your fixtures model:
attr_accessible :players_attributes, :opposition, :team_id, :venue_id, :date
This allows rails to set these attributes via new and update_attributes methods.
See the rails guide on security for more information.

Resources