NoMethodError on a form create method - ruby-on-rails

I am getting a weird error after adding <%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %> inorder to add a datetime selector.
I am wondering, what could the problem be?
NoMethodError in Messages#new
and
undefined method ``schedule' for #<Message:0x000000066fa1e8>
my create controller is
def create
#lists = current_user.lists.all
#message = Message.new(params[:message])
lists = params[:message][:lists]
schedule = params[:message][:schedule]
if #message.save
if schedule == [""]
MessageWorker.perform_async(#message.id, lists, current_user.id)
else
Schedule.create(execution_time: schedule, lists: lists, message_id: #message_id, user_id: current_user.id)
end
else
render action: "new"
flash[:notice] = "Messages Not Sent"
end
end
and the view is;
<%= simple_form_for(#message, :html => {:multipart => true}) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :from, placeholder: "Max 11 characters" , input_html: {id: 'sms_from'} %>
<%= f.input :to, :input_html => {:value => params[:to], :class => "to"}, placeholder: "Separate Numbers with Comma", hint: "Include atleast one number in this section. Separate the numbers with commas." %>
<div id="add_lists"> <span class="button"> Add List </span></div>
<div id="all_lists"> <%= f.collection_check_boxes :lists, #lists, :id, :name, as: :select %></div>
<%= f.input :message, :as => :text, :input_html => {:cols => 60, :rows => 7, id: "compose_message", placeholder: 'Type your message here!'} %>
<div>
<p>
<span id="char_count">160 characters left</span>
<span id="number_of_messages">1 message(s)</span>
</p>
</div>
<%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %>
<%= f.input :user_id, :as => :hidden, :input_html => {:value => #current_user.id} %>
<%= f.input :status, :as => :hidden, :input_html => {:value => "Queued"} %>
<%= f.button :submit, "Send Message" %>
<% end %>
and the model top looks like this;
class Message < ActiveRecord::Base
attr_accessible :message, :phone, :status, :to, :from, :user_id, :schedule
validates :message, :presence => true
validates :from, :presence => true
validates :to, :presence => true
validates :status, :presence => true
validates_length_of :message, :maximum => 1600, :allow_blank => true
validates_length_of :from, :maximum => 11, :allow_blank => false
belongs_to :user
Everything was working well until i added;
<%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %>

Did you migrate the database to add the schedule column?
Or if you are not storing :schedule in the database, then you need to add:
attr_accessor :schedule
to your Message model.

You don't have to use f.input, you can use f.date_field of f.datetime_field as outlined in the API: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-date_field
Also, you don't have to use the form helper in this way as you can use datetimefield_tag instead of f.datetime_field as outlined here http://rubydoc.info/docs/rails/ActionView/Helpers/FormTagHelper:datetime_field_tag

Related

How to input a Decimal value to a hidden simple form field - Ruby on Rails

I have an input form built with simple_form and I need to include a decimal value hiddenly.
The value type is decimal (t.decimal "price")
I've tried several ways but it is not saved as an input, the field keeps empty.
My form, with several attempts, no one succesfull:
<%= simple_form_for #post do |form| %>
<%= form.input :price, :as => "hidden", :input_html => {:type => "decimal", :value => 20 } %>
<%= form.input :price, :as => "hidden", :input_html => { :value => "20" } %>
<%= form.input :price, :as => "hidden", :input_html => { :value => 20 } %>
<% end %>
I am pretty sure it is a syntax problem, but just in case my model contains:
validates :price, presence: true
I know the decimal value is not saving because when submitting the form it checks if #post.valid? and it launches can't be blank error to the price field
Perhaps this will work:
<%= simple_form_for(#post) do |form| %>
<%= form.input :price, as: :hidden, :input_html => { :value => '20' } %>
<% end %>

Can't get Dropdown Association to work with simple_form

I'm using simple_form for my forms and I have a landing page which I have associated to a template. I'm trying to have a dropdown appear on the landing page form that lets the user select the template for the landing page and have the template names appear in the dropdown.
Here are my models:
landing_page.rb
class LandingPage < ActiveRecord::Base
belongs_to :template
has_many :leads
accepts_nested_attributes_for :template
validates_presence_of :name
validates_presence_of :title
validates_presence_of :page_url
validates_presence_of :template_id
validates_presence_of :content
end
template.rb
class Template < ActiveRecord::Base
has_many :landing_pages
validates_presence_of :template_name
end
Here is my landing page form:
<%= simple_form_for(#landing_page) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name, :input_html => {:maxlength =>50, :style=> 'width: 500px'} %>
<%= f.input :title, :input_html => {:maxlength =>50, :style=> 'width: 500px'}, label: 'HTML Title' %>
<%= f.input :page_url, :input_html => {:maxlength =>50, :style=> 'width: 500px'} %>
<%= f.input :template_id, :input_html => {:maxlength =>50, :style=> 'width: 500px'} %>
<%= f.input :content, :input_html => {:maxlength =>50, :style=> 'width: 500px'} %>
</div>
If I enter an integer for the template_id it's saved to the database correctly. However, I've tried every page I could find with examples and can't seem to make a dropdown (collection select) work for this field and have it display the template names in the dropdown.
I've looked at these pages:
http://simple-form.plataformatec.com.br/
https://github.com/plataformatec/simple_form
How to have a collasped drop down list in rails simple_form
https://github.com/plataformatec/simple_form/wiki/Nested-Models
Any help is greatly appreciated.
I got this to work:
<%= f.association :template, label_method: :template_name, value_method: :id, include_blank: true, :input_html => {:maxlength =>50, :style=> 'width: 500px'} %>

How to NOT save both parent and child record when associated child record is not valid in rails 3.1.4?

In our rails 3.1.4 app, there are user (parent) and user_levels (child) models.
class User < ActiveRecord::Base
attr_accessible :name, :login, :password, :user_levels_attributes, :as => :role_new
has_many :user_levels
ccepts_nested_attributes_for :user_levels, :allow_destroy => true, :reject_if => proc { |a| a['position'].blank? }
validates_associated :user_levels
end
class UserLevel < ActiveRecord::Base
belongs_to :user
validates_presence_of :position
end
The position column in user_level has to be filled. If it is not, both user and user_levels should NOT be saved. The problem with the above is that it always causes error of "position can't be blank" even there is a position value in the params.
{"utf8"=>"✓",
"user"=>{"name"=>"tester ceo",
"login"=>"testceo",
"update_password_checkbox"=>"[FILTERED]",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"user_levels_attributes"=>{"1339886115748"=>{"position"=>"ceo"}}},
"commit"=>"Save"}
After deleting user_levels_attribues, the model saves user into users table no matter what kind of user_levels is filled int. We did extensive online search and haven't found a solution yet. For example, if validates_presence_of :user_levels is added to the user model, then none of the user levels can be saved or updated, even with a valid position.
Any suggestion about how to implement the orchestrated saving/updating of two associated models? Thanks so much.
UPDATE:
_user_level.html.erb:
<div class="fields">
<%= f.input :position, :collection => return_position, :prompt => "Choose position",
:label => false, :include_blank => true, :selected => i_id %>
<%= link_to_remove_fields "remove", f %>
</div>
_form_new.html.erb:
<%= simple_form_for #user do |f| %>
<%= f.input :name, :label => 'name:' %>
<%= f.input :login, :label => 'login:', :hint => '6 digits ' %>
<%= f.input :password, :label => 'password:', :hint => '6digits', :input_html => {:id => 'new_user_password'} %>
<%= f.input :password_confirmation, :label => 'confirmation:' %>
<%= f.input :user_type, :label => 'user type:', :collection => return_user_type, :include_blank => true %>
position:
<p><%= link_to_add_fields "Choose position", f, :user_levels %></p>
<p><%= f.button :submit, 'Save' %></p>
<% end %>
Here is the method to add position field:
#add user position in system user creation
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render :partial => association.to_s, :locals => {:f => builder, :i_id => 0}
end
link_to_function(name, "add_fields(this, \"#{association}\", \"#{j fields}\")")
end
JS file:
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
I think you need to remove the :reject_if and change validates_associated :user_levels to validates_presence_of :user_levels.

Rails 3.1+ Simple Nested Form validation and Twitter Bootstrap control states

I need some help with handling validations with Twitter Bootstrap properly. The validations are working properly, the issue is Twitter Bootstrap Flash screen. This is what I mean:
No fields filled out and submitted returns: http://i.stack.imgur.com/8pvUc.png
The 2 of the required 4 fields submitted returns: http://i.stack.imgur.com/J6lCi.png
(notice that it doesn't flash the errors)
I have a basketball app where a Player can be on many Rosters(for player roster archiving purposes) and a Roster can have many Players.
Roster.rb
class Roster < ActiveRecord::Base
belongs_to :team
has_many :rosterizes
has_many :players, :through => :rosterizes
accepts_nested_attributes_for :players
validates_presence_of :jersey_number, :class_year
attr_accessible :jersey_number, :class_year, :players, :team_id, :players_attributes
end
Rosterizes.rb(poorly named I know...)
class Rosterize < ActiveRecord::Base
belongs_to :player
belongs_to :roster
attr_accessible :player_id, :roster_id
end
Player.rb
class Player < ActiveRecord::Base
has_many :rosterizes
has_many :rosters, :through => :rosterizes
validates_presence_of :first_name, :last_name
attr_accessible :first_name, :last_name, :active
end
_add_player_form.html.erb
(nested form)
<%= simple_nested_form_for #roster, :url =>player_added_team_path, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.simple_fields_for :players, #roster.players.build do |x| %>
<%= x.input :first_name %>
<%= x.input :last_name %>
<%= x.input :active, :as => :hidden, :input_html => {:value => true} %>
<%end%>
<%=f.input :class_year %>
<%=f.input :jersey_number %>
<%=f.input :team_id, :as => :hidden, :input_html => {:value => params[:id]}%>
<div class="well">
<%= f.button :submit, :class => 'btn-primary icon-plus-sign btn-success', :value => "Add To Team" %>
</div>
<%end%>
Thanks for the help in advance!
SOLUTION
My problem is that every time the view was loaded, it was building a new form, thus never getting back the errors on the rollback
I made a change to my controller that builds for the nested form
Controller
def add_player
...
#new_player = #roster.players.build
end
And made the change accordingly to the view
_add_player_form.html.erb
<%= f.simple_fields_for :players, #new_player do |x| %>
...
<%end%>
This did the trick, thanks Mober!
the reason why only the first level validation is shown an the associated not is fairly simple... You do a <%= f.simple_fields_for :players, #roster.players.build do |x| %> which buildes the association ALWAYS new when the sites's rendered. What you want to is building the association only if it does not exists yet...
<% #roster.players.build if !#roster.players %>
<%= f.simple_fields_for :player do |player_form| %>
...
<% end %>
Twitter Bootstrap doesn't actually validate anything, it just provides styling for validation classes (error, success, etc).
Now, I'm not a Ruby expert by any means but it looks like you are ending your form early:
<%= f.simple_fields_for :players, #roster.players.build do |x| %>
<%= x.input :first_name %>
<%= x.input :last_name %>
<%= x.input :active, :as => :hidden, :input_html => {:value => true} %>
<%end%> <---- here
<%=f.input :class_year %>
<%=f.input :jersey_number %>
<%=f.input :team_id, :as => :hidden, :input_html => {:value => params[:id]}%>
Should be this?
<%= f.simple_fields_for :players, #roster.players.build do |x| %>
<%= x.input :first_name %>
<%= x.input :last_name %>
<%= x.input :active, :as => :hidden, :input_html => {:value => true} %>
<%= f.input :class_year %>
<%= f.input :jersey_number %>
<%= f.input :team_id, :as => :hidden, :input_html => {:value => params[:id]}%>
<% end %>

Passing post_id for comment through comments controller

I have two models with a belongs_to/has_many relationship. Posts have many comments, comments belong to posts.
I need to pass the post_id through comments_controller.rb#new.
def new
#post = Post.find(params[:post_id])
#comment = Comment.new(:parent_id => params[:parent_id], :post_id => params[:post_id])
end
comment form:
<%= simple_form_for([#post, #post.comments.new]) do |f| %>
<%= f.input :post_id, :required => false, :as => :hidden %>
<%= f.input :parent_id, :required => false, :as => :hidden %>
<%= f.input :name, :label => false, :placeholder => "Name (optional)", :required => false %>
<%= f.input :content, :label => false, :placeholder => "Reply", :as => :text %>
<%= f.button :submit, "Reply" %>
<% end %>
You can pass a params from view to a controller method but I don't think you can pass a params to a controller then to a view.
In your new action, you may have to declare a #variable and define it to be your params where you can use it in your view.
But, you have already have #post.id, why can't you just use that?

Resources