Ruby on rails nested form model - ruby-on-rails

I'm trying to use rails nested form_for helper, but I am getting the following error:
BlogPage(#49859550) expected, got Array(#31117360)
Here are my model objects:
class Blog < ActiveRecord::Base
# Table Configuration
set_table_name "blog"
# Model Configuration
belongs_to :item
has_many :blog_pages
accepts_nested_attributes_for :blog_pages, :allow_destroy => true
end
class BlogPage < ActiveRecord::Base
# Table Configuration
set_table_name "blog_page"
# Model Configuration
belongs_to :blog
end
Here is the form I generated (left out unnecessary HTML):
<% form_for :blog, :url => { :action => :create } do |blog_form| %>
<%= blog_form.text_field :title, :style => "width: 400px" %>
<% blog_form.fields_for :blog_pages do |page_fields| %>
<% #blog.blog_pages.each do |page| %>
<%= page_fields.text_area :content, :style => "width: 100%",
:cols => "10", :rows => "20" %>
<% end %>
<% end %>
<% end %>
Here are the parameters that are sent to the controller:
{"commit"=>"Save",
"blog"=>{"blog_pages"=>{"content"=>"This is the new blog entries contents."},
"title"=>"This is a new blog entry.",
"complete"=>"1"},
"authenticity_token"=>"T1Pr1g9e2AjEMyjtMjLi/ocrDLXzlw6meWoLW5LvFzc="}
Here is the BlogsController with the create action that gets executed:
class BlogsController < ApplicationController
def new
#blog = Blog.new # This is the line where the error gets thrown.
# Set up a page for the new blog so the view is displayed properly.
#blog.blog_pages[0] = BlogPage.new
#blog.blog_pages[0].page_number = 1
respond_to do |format|
format.html # Goes to the new.html.erb view.
format.xml { render :xml => #blog }
format.js { render :layout => false}
end
end
def create
#blog = Blog.new(params[:blog])
respond_to do |format|
if #blog.save
render :action => :show
else
flash[:notice] = "Error occurred while saving the blog entry."
render :action => :new
end
end
end
end
If anyone can help me with this I would greatly appreciate it. I'm still pretty new to ruby and the rails framework and couldn't solve the problem on my own by googling.
Thanks.

Have you seen this?
http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf

Change your form to this:
<% form_for :blog, :url => { :action => :create } do |blog_form| %>
<%= blog_form.text_field :title, :style => "width: 400px" %>
<% blog_form.fields_for :blog_pages do |page_fields| %>
<%= page_fields.text_area :content, :style => "width: 100%",
:cols => "10", :rows => "20" %>
<% end %>
<% end %>
If you use fields_for it iterates over blog_pages automaticaly. However I'm not sure if this caused errors.

Related

id nil has_many :through but in parameters exist :id

I have 3 models with has_many :through relation. Users, Events, and Galleries. In the method new and create in gallery_controller I need to get the event_id, however I get a nil event_id. But in the mozilla console and in the parameters, there exists the id. I don't what I am doing wrong?
I also want to know if the structure of new and create actions is ok ? I want add a gallery for a event before created and in the same time in the the current_user galleries, i have not can test it by the previous problem.
Thanks and cheers.
class Event < ActiveRecord::Base
has_many: galleries
has_many: users, through: : galleries, : source => : users, : dependent => : destroy
accepts_nested_attributes_for: users
accepts_nested_attributes_for: galleries
end
class User < ActiveRecord::Base
has_many :galleries
has_many :events, through: :galleries, :dependent => :destroy
accepts_nested_attributes_for :events
accepts_nested_attributes_for :galleriesenter code here
end
class Gallery < ActiveRecord::Base
has_many :pictures, :dependent => :destroy
belongs_to :event
belongs_to :user
end
Gallery_controller
def new
#event = Event.find(params[:event_id])
#galery = Gallery.new
respond_to do |format |
format.html# new.html.erb
format.json {
render json: #gallery
}
end
end
def create
#event = Event.find(params[:id])
#gallery = #event.galleries.build(params[:gallery])
#gallery.user = current_user
respond_to do |format |
if# gallery.save
if params[: images]# The magic is here;)
params[: images].each { | image | #gallery.pictures.create(image: image)
}
end
def gallery_params
params.require(:gallery).permit(:description,
:name,
:pictures,
:event_attributes => [],
:user_attributes => [],
)
end
form_ new gallery
<%= form_for [#event,#gallery], :html => { :class => 'form-horizontal', multipart: true } do |f| %>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :description, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :description, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :pictures, :class => 'control-label' %>
<div class="controls">
<%= file_field_tag "images[]", type: :file, multiple: true %>
</div>
</div>
<div class="form-actions">
<%= f.submit :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
galleries_path, :class => 'btn btn-mini' %>
</div>
<% end %>
routes
resources :events do
resources :galleries
end
Image error
http://i.stack.imgur.com/mk1Ti.png
The error is because you have a typo in your new method.
This line
#galery = Gallery.new
should be
#gallery = Gallery.new
Furthermore your create method has some mistakes which needs fixing.
def create
#event = Event.find(params[:event_id])
#gallery = #event.galleries.build(gallery_params)
#gallery.user = current_user
respond_to do |format|
if #gallery.save
if params[:images]
params[:images].each { |image| #gallery.pictures.create(image: image)}
end
format.html { redirect_to #gallery, notice: 'Gallery was successfully created.' }
format.json { render json: #gallery, status: :created, location: #gallery }
else
format.html { render :new }
format.json { render json: #gallery.errors, status: :unprocessable_entity }
end
end
end
And also your gallery_params needs tweaking
def gallery_params
params.require(:gallery).permit(:description,:name)
end
You don't want to include :event_attributes => [], :user_attributes => [] unless your form has nested fields for users and events which needs to be saved.
I think I found your problem. To me there seems more problems. But initially to solve your problem: you need add event_id to permit params methods:
params.require(:gallery).permit(:description,
:name,
:pictures,
:event_id, # this line should be here if your foreign key is event_id for gallery model.
:event_attributes => [],
:user_attributes => [],
)
Also your form doesn't content the right instance variable. Differences here:
<%= form_for [#event,#gallery], :html => { :class => 'form-horizontal', multipart: t # you wrote #gallery here but in your controller you wrote:
# controller's action:
#galery = Gallery.new
Suggested way to keep the foreign key hidden as well as you build from events also:
#event = Event.find(params[:event_id])
#gallery = #event.gallaries.build #note: 1.spelling and 2. building from #event object
Then in your form add the foreign key field as hidden:
<%= f.hidden_field :event_id %>

I don't get why this platformatic mail form contact form isn't working

models/lead.rb
class Lead < MailForm::Base
attribute :fullname
def headers
{
:subject => "My Contact Form",
:to => "callumshorty#hotmail.com",
:from => "admin#uk-franchise.co.uk"
}
end
end
controllers/lead_form_controller.rb
class LeadFormController < ApplicationController
def new
#lead = Lead.new
end
def create
#lead = Lead.new(params[:lead_form])
#lead.request = request
#lead.deliver
end
end
routes.rb
resources :lead_form
views/listings/show.html.erb
<%= form_for #lead, :url => url_for(:controller => 'lead_form', :action => 'new') do |lead| %>
<%= lead.text_field :fullname %>
<%= lead.submit %>
<% end %>
The error when trying to access the show page:
First argument in form cannot contain nil or be empty
On line:
<%= form_for #lead, :url => url_for(:controller => 'lead_form', :action => 'new') do |lead| %>
Any help would be super appreciated, can't stand these mailers :(
Your #lead needs to be initialised in the ListingsController#show action, since your lead form is in that view. Try adding #lead = Lead.new in the ListingsController#show method.

ActionController::ParameterMissing (param is missing or the value is empty: film):

So, I'm getting the following error when trying to visit the films page on my app:
ActionController::ParameterMissing (param is missing or the value is empty: film):
2014-07-24T22:04:44.622356+00:00 app[web.1]: app/controllers/saas_admin/films_controller.rb:54:in `permitted_params'
See my films controller code below
films_controller.rb
class SaasAdmin::FilmsController < SaasAdminController
inherit_resources
belongs_to :studio, :finder => :find_by_id!, :param => :studio_id, :class_name => Studio
before_filter :set_sort_fields, :only => :edit
before_filter :build_collections, :only => [:new, :create, :edit, :update]
def create
create! { parent_path(parent.id) } # Redirect to studio in case studio_id is changed
end
def update
#film = Film.find_by_permalink(params[:id])
respond_to do |format|
if #film.update(permitted_params)
format.html { redirect_to saas_admin_studio_path(#film.studio), notice: 'Film was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #film.errors, status: :unprocessable_entity }
end
end
end
def index
redirect_to parent_path(parent.id)
end
def show
#clips = resource.clips.paginate(:page => params[:page], :per_page => 30, :order => 'clips.position')
end
protected
def resource
# #film ||= end_of_association_chain.find_by_permalink!(params[:id])
#film ||= end_of_association_chain.find_by_permalink!(params[:id])
end
def collection
#films ||= end_of_association_chain.paginate(:page => params[:page], :per_page => 30, :order => 'films.created_at')
end
def set_sort_fields
resource.sort_name = '' if resource.name == resource.sort_name
end
def build_collections
#studios ||= Studio.find(:all)
end
def permitted_params
params.require(:film).permit(:name, :sort_name, :description, :short_description, :meta_data,
:poster, :amazon_link, :active, :trackable, :country_ids => [])
end
end
What might this be? I've been trying to figure it out for a bit but perhaps a fresh set of eyes will find it's something rather simple.
Cheers!
Edit
Here's the view code for films/new.html.erb
<h1><%= #page_title = "New #{resource_class}" %></h1>
<%= form_for resource, :url => collection_path, :html => { :multipart => true } do |f| -%>
<%= render :partial => "form", :locals => { :f => f } %>
<% end -%>
<% content_for :sidebar do %>
<%= render :partial => "saas_admin/shared/sidebar" %>
<% end %>
and films/edit.html.erb
<h1><%= #page_title = "Edit #{resource_class}" %></h1>
<%= form_for resource, :url => saas_admin_studio_film_path(parent, resource), :html => { :multipart => true } do |f| -%>
<%= render :partial => "form", :locals => { :f => f } %>
<% end -%>
<% content_for :sidebar do %>
<%= render :partial => "saas_admin/shared/sidebar" %>
<% end %>
Edit 2
For reference here is how the permitted params was defined when it was working:
def permitted_params
{:film => params.fetch(:film, {}).permit(
:name, :sort_name, :description, :short_description, :meta_data,
:poster, :amazon_link, :active, :trackable)}
end
I have got this problem too when I use Angular JS form to send data to backend Rails 4. When I did not fill anything in angular js form, the error will show ActionController::ParameterMissing (param is missing or the value is empty:.
I fix it by adding params.fetch(:film, {}) the strong parameter into:
params.fetch(:film, {}).permit(:name, :sort_name, :description, :short_description, :meta_data,
:poster, :amazon_link, :active, :trackable, :country_ids => [])
I refer to code example to avoid ActionController::ParameterMissing (param is missing or the value is empty: film)
I hope this will help you.
Why not use so:
def creation_params
params.permit(:film)
end
It working for me! ;)
This is happening because you have specified to require 'film' in your parameters through strong_params (specified above in your permitted_params code).
Whatever the view side is doing (whether its a link or a form/etc.), its not passing its parameters nested under 'film'
eg.) if you were to raise params.inspect in the controller action, you would see that there is no node for "film".
Most likely what is wrong is that the form code you have on the view side is not set to nest these parameters properly, are you using a form_tag for example?

Storing Array of Checkbox Selections

I have an attribute called "features" in my application. In my form, "features" consists of a list of check boxes. The idea here is that users can check off which "features" apply to their post, and that list of features gets saved into the record.
I see the array being saved in my console ("features"=>{"Private bathroom"=>"1", "Elevator"=>"0", "Complimentary breakfast"=>"1", "Great view"=>"1", "Cable TV"=>"0", "Fireplace"=>"0", "Other (see description)"=>"0", "Sweet location"=>"0"}).
However... When I view the record, features returns nil. It doesn't seem to be saving the features array.
Code provided below. Any idea what I'm doing wrong here?
models/accommodation.rb
class Accommodation < ActiveRecord::Base
validates_presence_of :title, :description, :thing, :location
attr_accessible :photo_attributes, :title, :description, :thing, :borough, :location, :spaces, :price, :features
has_one :photo
has_many :requests
belongs_to :user
accepts_nested_attributes_for :photo, :allow_destroy => true
end
controllers/accommodation_controller.rb
class AccommodationsController < ApplicationController
before_filter :auth, :except => :show
uses_tiny_mce ( :options => {
:theme => 'advanced',
:theme_advanced_toolbar_location => 'top',
:theme_advanced_toolbar_align => 'left',
:theme_advanced_buttons1 => 'bold,italic,underline,image,bullist,numlist,separator,undo,redo',
:theme_advanced_buttons2 => '',
:theme_advanced_buttons3 => ''
})
def show
#accommodation = Accommodation.find(params[:id])
end
def new
#accommodation = current_user.accommodations.build
#accommodation.build_photo
end
def create
#accommodation = current_user.accommodations.build(params[:accommodation])
if #accommodation.save
flash[:notice] = "Successfully created your accommodation."
redirect_to #accommodation
else
render :new
end
end
def edit
#accommodation = Accommodation.find(params[:id])
end
def update
#accommodation = Accommodation.find(params[:id])
if #accommodation.update_attributes(params[:accommodation])
flash[:notice] = "Successfully updated accommodation."
redirect_to #accommodation
else
render :edit
end
end
def destroy
#accommodation = Accommodation.find(params[:id])
#accommodation.destroy
flash[:notice] = "Successfully destroyed accommodation."
redirect_to :inkeep
end
private
def auth
if current_user
if params[:action] != 'new' && params[:action] != 'create'
#accommodation = Accommodation.find(params[:id])
if #accommodation.user_id != current_user.id
flash[:notice] = "You don't own this accommodation!"
render :action => 'show'
end
end
return true
else
flash[:error] = "Please login first."
redirect_to :controller => 'sessions', :action => 'new'
end
end
end
views/accommodations/_form.html.erb
<%= form_for #accommodation, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
Title<br />
<%= f.text_field :title, :size => 60 %>
</p>
<p>
Description<br />
<%= f.text_area :description, :rows => 17, :cols => 75, :class => "mceEditor" %>
</p>
[...snip...]
<p>
<i>Featuring...</i>
<%= fields_for :features do |feature_fields| %>
<table>
<tr>
<td><%= feature_fields.check_box 'Private bathroom' %> Private bathroom</td>
<td><%= feature_fields.check_box 'Cable TV' %> Cable TV</td>
<td><%= feature_fields.check_box 'Complimentary breakfast' %> Complimentary breakfast</td>
</tr>
<tr>
<td><%= feature_fields.check_box 'Elevator' %> Elevator</td>
<td><%= feature_fields.check_box 'Fireplace' %> Fireplace</td>
<td><%= feature_fields.check_box 'Great view' %> Great view</td>
</tr>
<tr>
<td><%= feature_fields.check_box 'Sweet location' %> Sweet location</td>
<td><%= feature_fields.check_box 'Other (see description)' %> Other (see description)</td>
</tr>
</table>
<% end %>
</p>
[...snip...]
<% end %>
First, is the features array inside of your the accommodation hash in the params hash?
Second, there is no db column type which accepts an array, so you need to put
serialize :features
in the model. This will store the array as yaml in the db. You can also specify the data type as an argument to serialize() (probably Array in this case), but it's not always necessary.
I add the same problem today, it appears the form isn't properly built in the view.
Indeed, take a closer look at your params: params[:features] is outside params[:accomodation]
I simply added at the beginning of my create action:
params[:accomodation][:features] = params[:features]
And it works properly
What about your model? Do you have attr_accessible or attr_protected calls in there?

Paperclip: "missing" image

I'm working on a website that allows people who run bed and breakfast businesses to post their accommodations.
I would like to require that they include a "profile image" of the accommodation when they post it, but I also want to give them the option to add more images later (this will be developed after).
I thought the best thing to do would be to use the Paperclip gem and have a Accommodation and a Photo in my application, the later belonging to the first as an association.
A new Photo record is created when they create an Accommodation. It has both id and accommodation_id attributes. However, the image is never uploaded and none of the Paperclip attributes get set (image_file_name: nil, image_content_type: nil, image_file_size: nil), so I get Paperclip's "missing" photo.
Any ideas on this one? It's been keeping me stuck for a few days now.
Accommodation
models/accommodation.rb
class Accommodation < ActiveRecord::Base
validates_presence_of :title, :description, :photo, :thing, :location
attr_accessible :title, :description, :thing, :borough, :location, :spaces, :price
has_one :photo
end
controllers/accommodation_controller.erb
class AccommodationsController < ApplicationController
before_filter :login_required, :only => {:new, :edit}
uses_tiny_mce ( :options => {
:theme => 'advanced',
:theme_advanced_toolbar_location => 'top',
:theme_advanced_toolbar_align => 'left',
:theme_advanced_buttons1 => 'bold,italic,underline,bullist,numlist,separator,undo,redo',
:theme_advanced_buttons2 => '',
:theme_advanced_buttons3 => ''
})
def index
#accommodations = Accommodation.all
end
def show
#accommodation = Accommodation.find(params[:id])
end
def new
#accommodation = Accommodation.new
end
def create
#accommodation = Accommodation.new(params[:accommodation])
#accommodation.photo = Photo.new(params[:photo])
#accommodation.user_id = current_user.id
if #accommodation.save
flash[:notice] = "Successfully created your accommodation."
render :action => 'show'
else
render :action => 'new'
end
end
def edit
#accommodation = Accommodation.find(params[:id])
end
def update
#accommodation = Accommodation.find(params[:id])
if #accommodation.update_attributes(params[:accommodation])
flash[:notice] = "Successfully updated accommodation."
render :action => 'show'
else
render :action => 'edit'
end
end
def destroy
#accommodation = Accommodation.find(params[:id])
#accommodation.destroy
flash[:notice] = "Successfully destroyed accommodation."
redirect_to :inkeep
end
end
views/accommodations/_form.html.erb
<%= form_for #accommodation, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
Title<br />
<%= f.text_field :title, :size => 60 %>
</p>
<p>
Description<br />
<%= f.text_area :description, :rows => 17, :cols => 75, :class => "mceEditor" %>
</p>
<p>
Photo<br />
<%= f.file_field :photo %>
</p>
[... snip ...]
<p><%= f.submit %></p>
<% end %>
Photo
The controller and views are still the same as when Rails generated them.
models/photo.erb
class Photo < ActiveRecord::Base
attr_accessible :image_file_name, :image_content_type, :image_file_size
belongs_to :accommodation
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "150x150>" }
end
To create an upload with paperclip, you need to use the name you provided for the has_attached_file line, on the model you defined it on. In your case, this will result in this view code:
<%= form_for #accommodation, :html => { :multipart => true } do |f| %>
<%= f.fields_for :photo do |photo_fields| %>
<p>
Photo<br />
<%= photo_fields.file_field :image %>
</p>
<% end %>
<% end %>
In the controller:
class AccommodationsController < ApplicationController
# also protect create and update actions!
before_filter :login_required, :only => [ :new, :create, :edit, :update ]
def new
# always make objects through their owner
#accommodation = current_user.accommodations.build
#accommodation.build_photo
end
def create
#accommodation = current_user.accommodations.build(params[:accommodation])
if #accommodation.save
# always redirect after successful save/update
redirect_to #accommodation
else
render :new
end
end
end
Tell Rails to handle the nested form:
class Accommodation
has_one :photo
accepts_nested_attributes :photo
attr_accessible :photo_attributes, :title, :description, :etc
end
And make sure to set the accessible attributes right in your photo model:
class Photo
attr_accessible :image # individual attributes such as image_file_name shouldn't be accessible
has_attached_file :image, :styles => "etc"
end
Be sure to watch your log files to spot things that are protected by attr_accessible, but still are in your form.

Resources