rails carrierwave gem error - ruby-on-rails-3

I have succsfuly set up an image upload with the carrierwave gem.
but when I try to add an optional online url like so:
<%= form_for #rating, :html => {:multipart=>true} do |f| %>
<div class="field">
<%= f.file_field :pic_url %>
</div>
<div class="field">
<%= f.label :remote_pic_url_url, 'or image url' %>
<br/>
<%= f.text_field :remote_pic_url_url %>
</div>
<div class="actions">
<%= f.submit 'Upload Picture', :class => 'btn btn-primary' %>
</div>
<% end %>
then I get this error:
Can't mass-assign protected attributes:
my model is
class Rating < ActiveRecord::Base
attr_accessible :pic_url, :rating
mount_uploader :pic_url , ImageUploader
end

You need to be able to mass-assign the remote_pic_url_url attribute:
class Rating < ActiveRecord::Base
attr_accessible :pic_url, :remote_pic_url_url, :rating
mount_uploader :pic_url , ImageUploader
end

Related

Devise nested form not creating profile

Rails 5.0.1
Devise 4.2.0
Hello, I'm building an app in which the user must fill a form to sign up, which includes the registration info for devise, and some personal info por their profile. I have made the models for user and profile, established their one to one relation, and added accept_nested_attributes_for :profile inside the user. I have also modified the registrations views to include f.fields_for for the profile, and until that point everything seems to work fine.
But, when I try to create a new user, and fill the required information, I get an error inside the view (from devise I guess) saying:
1 error prohibited this user from being saved
Profile user must exist
I have already followed many guides on how to create a nested form with devise, and none of them seem to have this issue, and I have also searched a lot with no answer. Here are some snippets from my registration controller, user and profile model, and registrations/new view:
registrations_controller
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
before_action :configure_account_update_params, only: [:update]
def new
build_resources({})
resource.build_profile
respond_with self.resource
end
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up,
keys: [:email, :password,
:password_confirmation,
profile_attributes: [:name, :art_name,
:birth_date, :location]])
end
User model
class User < ApplicationRecord
has_one :profile, dependent: :destroy
before_create :build_profile
accepts_nested_attributes_for :profile
# Devise configuration.....
end
Profile model
class Profile < ApplicationRecord
belongs_to :user
end
Registrations/new view
<h2>Nueva Cuenta</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.fields_for :profile do |b| %>
<div class="field">
<%= b.label :name, "Nombre" %><br />
<%= b.text_field :name %>
</div>
<div class="field">
<%= b.label :art_name, "Nombre artistico" %><br />
<%= b.text_field :art_name %>
</div>
<div class="field">
<%= b.label :birth_date, "Fecha de nacimiento" %><br />
<%= b.date_field :birth_date %>
</div>
<div class="field">
<%= b.label :location, "Ubicacion" %><br />
<%= b.text_field :location %>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
My guess is that it has something to do with the sanitizers, I'm not completely sure how do I pass each attribute through, since most of the guides that explained this did with an older version of devise.
Thanks for your attention.
in your profile model, try this:
belongs_to :user, optional: true
it works for me

learning rails, trying to build blog categorization

I am working on building a blog with categorization. I am a bit stuck on how to implement categorization in the form. I have setup a has many through relationship and want to add check boxes to associate a blog with multiple categories. What I have so far is passing the categories through to the view, and I can list them out, however I cannot get the form_for method working for some reason.
Here is my code.
blog model
class Blog < ActiveRecord::Base
attr_accessible :body, :title, :image
has_many :categorizations
has_many :categories, through: :categorizations
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates :title, :body, :presence => true
end
Category Model
class Category < ActiveRecord::Base
has_many :categorizations
has_many :blogs, through: :categorizations
attr_accessible :name
end
Categorization Model
class Categorization < ActiveRecord::Base
attr_accessible :blog_id, :category_id
belongs_to :blog
belongs_to :category
end
Blog new controller
def new
#blog = Blog.new
#categories = Category.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: #blog }
end
end
Blog new form view
<%= form_for(#blog, :url => blogs_path, :html => { :multipart => true }) do |f| %>
<% if #blog.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#blog.errors.count, "error") %> prohibited this blog from being saved:</h2>
<ul>
<% #blog.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.file_field :image %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="field">
Categories:
<% #categories.each do |category| %>
<% fields_for "blog[cat_attributes][]", category do |cat_form| %>
<p>
<%= cat_form.check_box :name %>
</p>
<% end %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This code is my failing point
<% #categories.each do |category| %>
<% fields_for "blog[cat_attributes][]", category do |cat_form| %>
<p>
<%= cat_form.check_box :name %>
</p>
<% end %>
<% end %>
Although I am not positive I am approaching any of it right since I am currently learning. Any advice on how to accomplish this.
Thanks,
CG
First of all, you probably don't need a separate Categorization model unless there's a use case you haven't described here. You can set up a many-to-many relationship like this:
class Blog < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :blogs
end
You should have a database table like this:
class CreateBlogsCategories < ActiveRecord::Migration
def change
create_table :blogs_categories, id: false do |t|
t.references :blog
t.references :category
end
end
end
Then you can construct the view like this:
<div class="field">
Categories:
<% #categories.each do |category| %>
<%= label_tag do %>
<%= check_box_tag 'blog[category_ids][]', category.id, #blog.category_ids.include?(category.id) %>
<%= category.name %>
<% end %>
<% end %>
</div>
Lastly, in your form_for, you specify url: blogs_path - you should remove this if you plan to use this form for the edit action as well, because that should generate a PUT request to /blogs/:id. Assuming you used resources :blogs in routes.rb, Rails will determine the correct path for you based on the action used to render the form.

Nested Form doesn't show assignments

I recently followed a tutorial about nested forms by Ryan Bates and did basically the same thing as he did just with other names. I wanted to nest assignments in the order form and I would like to build an assignment for every bun to that order and the user should put a count in the form.
So my controller looks like this
def new
#order = Order.new
#buns = Bun.all
#buns.each do |bun|
#order.assignments.build(:bun_id => bun.id)
end
end
And the _form partial looks like this
<%= form_for(#order) do |f| %>
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<div id="assignments" class="field">
<% f.fields_for :assignments do |builder| %>
<div id="assignment" class="field">
<%= builder.label :count, "Anzahl" %>
<%= builder.text_field :count %>
<%= builder.object.bun_id %>
<% end %>
</div>
</div>
<div class="field">
<%= f.label :due_to %><br />
<%= f.datetime_select :due_to %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The Order model has this part
has_many :assignments
has_one :user
accepts_nested_attributes_for :assignments
And the Assignments model this one:
attr_accessible :bun_id, :order_id, :count
belongs_to :bun
belongs_to :order
As i log the assignments out there are all, which should be build, so why weren't the fields rendered?
Thanks for your help!
You are missing an equal sign (=) in your erb :
<%= f.fields_for :assignments do |builder| %>

Create two sub-objects in a nested form?

In my rails app, I am creating an object "Organization" and two sub-objects "User" and "Settings" in a nested form. When I run the form I get the error:
WARNING: Can't mass-assign protected attributes: user
I had previously had this problem on another project and discovered I needed the
#organization.users.build
in organizations_controller/new. In order to have the sub-objected added to and create a a sub-object on submittal of the form.
organization.rb
class Organization < ActiveRecord::Base
attr_accessible :org_name, :address1, :address2, :city, :state, :postal_code, :country, :logo_image, :setting_id, :active, :status_image, :users_attributes, :setting_attributes
has_many :users, :dependent => :destroy
has_one :setting, :dependent => :destroy
accepts_nested_attributes_for :users
accepts_nested_attributes_for :setting
end
user.rb
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :name_first, :name_last, :admin, :notes, :current_outstanding, :pin, :registered, :sms, :super_user, :organization_id
belongs_to :organization
end
setting.rb
class Setting < ActiveRecord::Base
attr_accessible :green_hours, :green_mins, :yellow_hours, :yellow_mins, :red_hours, :red_mins, :reminder_sms, :reminder_sms_hours, :reminder_sms_mins, :logo_url, :comp_name, :alert_emails, :alert_smss, :history_hours, :time_zone, :user_id, :organization_id
belongs_to :organization
end
organizations_controller.rb
class OrganizationsController < ApplicationController
def new
#organization = Organization.new
#organization.build_setting
#organization.users.build
respond_to do |format|
format.html { render :layout => 'application_dashboard_orgs' }
format.json { render json: #organization }
end
end
def create
#organization = Organization.new(params[:organization])
respond_to do |format|
if #organization.save
format.html { redirect_to organizations_path, notice: 'Organization was successfully created.' }
format.json { render json: #organization, status: :created, location: #organization }
else
format.html { render action: "new" }
format.json { render json: #organization.errors, status: :unprocessable_entity }
end
end
end
end
organizations/_form.html.erb
<%= form_for(#organization) do |f| %>
<div class="Col1">
<div class="MenuItem"><div class="MenuItemLabel">Account Name:</div> <%= f.text_field :org_name %></div>
<div class="MenuItem"><div class="MenuItemLabel">Address Line 1:</div> <%= f.text_field :address1 %></div>
<div class="MenuItem"><div class="MenuItemLabel">Address Line 2:</div> <%= f.text_field :address2 %></div>
<div class="MenuItem"><div class="MenuItemLabel">City:</div> <%= f.text_field :city %></div>
<div class="MenuItem"><div class="MenuItemLabel">State:</div> <%= f.text_field :state %></div>
<div class="MenuItem"><div class="MenuItemLabel">Postal Code:</div> <%= f.text_field :postal_code %></div>
<div class="MenuItem"><div class="MenuItemLabel">Country:</div> <%= f.text_field :country %></div>
<div class="MenuItem"><div class="MenuItemLabel">Logo Image:</div> <%= f.file_field :logo_image %></div>
<div class="MenuItem"><div class="MenuItemLabel">Active Account?</div> <%= f.check_box :active %></div>
</div>
<div class="Col2">
<div class="MenuItem"><div class="MenuItemLabel"><b>Administrator Account</b></div></div>
<%= f.fields_for :user do |user| %>
<div class="MenuItem"><div class="MenuItemLabel">First Name:</div> <%= user.text_field :name_first %></div>
<div class="MenuItem"><div class="MenuItemLabel">Last Name:</div> <%= user.text_field :name_last %></div>
<div class="MenuItem"><div class="MenuItemLabel">Email Address:</div><%= user.text_field :email %></div>
<div class="MenuItem"><div class="MenuItemLabel">PIN:</div><%= user.password_field :password, :id => "password_field", :onchange => "add_pin()" %></div>
<div class="MenuItem"><div class="MenuItemLabel">PIN Confirmation:</div><%= user.password_field :password_confirmation %> </div>
<div class="MenuItem"><div class="MenuItemLabel">SMS Reminder Phone #:</div><%= user.text_field :sms %> </div>
<div class="MenuItem"><div class="MenuItemLabel">Is Administrator:</div> <%= user.check_box :admin, {checked: true} %></div>
<div class="MenuItem"><div class="MenuItemLabel">Password Setup?</div> <%= user.check_box :registered %></div>
<% end %>
<%= f.fields_for :setting do |setting| %>
<%= setting.hidden_field :green_hours, :value => params[99] %>
<%= setting.hidden_field :green_mins, :value => params[59] %>
<%= setting.hidden_field :yellow_hours, :value => params[0] %>
<%= setting.hidden_field :yellow_mins, :value => params[15] %>
<%= setting.hidden_field :red_hours, :value => params[0] %>
<%= setting.hidden_field :red_mins, :value => params[0] %>
<%= setting.hidden_field :reminder_sms, :value => params[true] %>
<%= setting.hidden_field :reminder_sms_hours, :value => params[0] %>
<%= setting.hidden_field :reminder_sms_mins, :value => params[15] %>
<%= setting.hidden_field :history_hours, :value => params[24] %>
<% end %>
<div class="MenuItem">
<%= flash[:notice] %> <br />
<%= f.submit "Save Settings" %> <!--<input type="reset" value="Reset" /> -->
</div>
<div class="clr"></div>
</div>
<%= f.fields_for :user do |user| %>
should be
<%= f.fields_for :users do |user| %>
I think what you want to do is use a form backed object. Jeff Dean has a good blog post on this at http://pivotallabs.com/users/jdean/blog/articles/1706-form-backing-objects-for-fun-and-profit
Your error indicates that you don't have the ability to set the "user" attribute when you try to save your organization. Try adding :user and :setting to your attr_accessible line in organization.rb

Help to render a partial from fields_for

I'm using nested_attributes and trying to implement the add/remove fields on-the-fly throu ajax following Ryan Bates screencast about Nested Model (#196)
Doing this, it won't work, but when removing the "link_to_add_fields" line, it works fine.
The problem is that I'm don't know if I doing this all associations right.
<%= form_for(#item) do |f| %>
<% if #item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% #item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :item_type_id %><br />
<%= f.select :item_type_id, #item_types.collect { |p| [ p.title, p.id ]} %>
<br />
<%= f.fields_for :item_parts do |parts_form| %>
<%= render "item_part_fields", :p => parts_form %>
<% end %>
</div>
<%= link_to_add_fields "Add Part", f, :part %>
<div class="actions">
<%= button_for "Save Item", :class => 'positive pill button', :button_type => 'submit' %>
</div>
<% end %>
Actually my models are:
"ItemPart" model:
class ItemPart < ActiveRecord::Base
belongs_to :item
belongs_to :part
end
"Item" model:
class Item < ActiveRecord::Base
belongs_to :item_type
has_many :item_parts
has_many :parts, :through => :item_parts
accepts_nested_attributes_for :item_parts, :allow_destroy => true
after_save :save_id
validates :title, :presence => true
def save_id
item_part_attributes = [ { :item_id => self.id } ]
end
end
"Part" model:
class Part < ActiveRecord::Base
has_one :part_type
has_many :item_parts
has_many :items, :through => :item_parts
end
The error I'm getting doing this way:
undefined method `klass' for nil:NilClass
Extracted source (around line #26):
23: <%= render "item_part_fields", :p => parts_form %>
24: <% end %>
25: </div>
26: <%= link_to_add_fields "Add Part", f, :item %>
27: <div class="actions">
Application trace
app/helpers/application_helper.rb:44:in `link_to_add_fields'
app/views/items/_form.html.erb:26:in `block in _app_views_items__form_html_erb__1418129287024756954_2169222480__3228169100620818569'
app/views/items/_form.html.erb:1:in `_app_views_items__form_html_erb__1418129287024756954_2169222480__3228169100620818569'
app/views/items/edit.html.erb:3:in `_app_views_items_edit_html_erb___1857878264245794505_2169270380_890290209246324491'
Application_helper.rb
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new # error line :44
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')" )
end
After hours and hours doing tests and googling a lot, I get it finally solved.
Seems that I'd to use the same local variable name to render the partial used for the form_for and then use it again as a helper method parameter needed. (In this case, "f" variable.)
I pasting the cleaning working code below
<%= form_for(#item) do |f| %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :item_type_id %><br />
<%= f.select :item_type_id, #item_types.collect { |p| [ p.title, p.id ]} %><br />
</p>
<p>
<%= f.fields_for :item_parts do |parts_form| %>
<%= render "item_part_fields", :f => parts_form %>
<% end %>
</p>
<p><%= link_to_add_fields "Add Part", f, :item_parts %></p>
<p><%= button_for "Save Item", :class => 'positive pill button', :button_type => 'submit' %></p>
<% end %>
Hope this going to help someone someday :)
There is a mistake in your add more button. This should be,
<p><%= link_to_add_fields "Add Part", f, :item_parts %></p>
The association name should be the table name on which have many relation is.