Rails 3 Nested Attributes - ruby-on-rails-3

I've had this issue for a few days and looked over every tutorial and stack overflow. I cannot seem to resolve. I can't seem to use nested attributes that work. I get an error with this form view.
**Form View**
<%= form_for #trip, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :start, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :start, :class => 'text_field' %>
</div>
<div class="control-group">
<%= f.label :end, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :end, :class => 'text_field' %>
</div>
<div class="control-group">
<% f.fields_for :driver do |d| -%>
<%= d.label :driver, :class => 'control-label' %>
<div class="controls">
<%= d.text_field :driver, :class => 'text_field' %>
</div>
Model:
class Trip < ActiveRecord::Base
attr_accessible :end, :start
belongs_to :driver
belongs_to :customer
accepts_nested_attributes_for :driver
end
class Driver < ActiveRecord::Base
attr_accessible :name
has_many :trips
end

As you said you need closing end tag.
If you have the "Can't mass-assing procteted attributes: drivers" error you must declare "attr_accessible" on :drivers_attributes at the trip model.

Related

Routing Error No route matches [POST]"/contacts/new

I seem to be stuck on a Ruby on Rails tutorial. I keep getting the error that is in the title of this post, which is:
Routing Error No route matches[POST]"/contacts/new"
app/views/contacts/new.html.erb
<div class="container">
<div class="rpw">
<h3 class="text-center">Contact Us</h3>
<div class="col-md-4 col-md-offset-4">
<%= flash[:notice] %>
<div class="well">
<%= form_for "/contacts" do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-success' %>
<% end %>
</div>
</div>
</div>
routes.rb
Rails.application.routes.draw do
root to: 'pages#home'
get 'about', to: 'pages#about'
resources :contacts
end
contacts_controller.rb
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to new_contact_path, notice: "Message sent."
else
redirect_to new_contact_path, notice: "Error occured"
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
contact.rb
class Contact < ActiveRecord::Base
end
I am unsure where I am going wrong. Please help me. I have been trying to fix this for a few days now.
In new.html.erb
try changing
<%= form_for "/contacts" do |f| %>
to
<%= form_for #contact do |f| %>

rails carrierwave upload image or image url error

I have installed the carrierwave gem.
my model is
class Rating < ActiveRecord::Base
attr_accessible :pic_url, :rating
mount_uploader :pic_url , ImageUploader
end
and my view for is
<%= 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 %>
I get an error msg Can't mass-assign protected attributes: remote_pic_url_url
when I remove the optional pic url field , then It works.
Just add remote_file_url to attr_accessible.

PUT routes broken by scope

In my application, I have several resources scoped into 'admin' as follows (in routes.rb):
scope 'admin', :as => 'admin' do
resources :events
end
So when I go in to use the #new or #update methods through the form, I end up getting the ActionController exception saying No route matches [PUT] "/admin/events".
rake routes produces the following:
admin_events GET /admin/events(.:format) events#index
POST /admin/events(.:format) events#create
new_admin_event GET /admin/events/new(.:format) events#new
edit_admin_event GET /admin/events/:id/edit(.:format) events#edit
admin_event GET /admin/events/:id(.:format) events#show
PUT /admin/events/:id(.:format) events#update
DELETE /admin/events/:id(.:format) events#destroy
_form code:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<%= form_for #event, :html => { :class => 'form-horizontal' }, :url => url_for(:controller => 'events', :action => 'index') do |f| %>
<fieldset>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name %>
</div>
</div>
<div class="control-group">
<%= f.label :event_date, :class => 'control-label' %>
<div class="controls date-selects">
<%= f.datetime_select :event_date, :start_year => 2012 %>
</div>
</div>
<div class="control-group">
<%= f.label :publish_date, :class => 'control-label' %>
<div class="controls date-selects">
<%= f.datetime_select :publish_date, :start_year => 2012 %>
</div>
</div>
<div class="control-group">
<%= f.label :blurb, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :blurb, :class => 'span6 wysi' %>
</div>
</div>
<div class="control-group">
<%= f.label :graphic, :class => 'control-label' %>
<div class="controls">
<%= f.file_field :graphic %>
</div>
</div>
<div class="control-group">
<%= f.label :tix_link, :class => 'control-label' %>
<div class="controls">
<%= f.url_field :tix_link %>
</div>
</div>
<div class="form-actions">
<%= f.submit 'Submit', :class => 'btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")), admin_events_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>
What am I doing wrong?
Try changing your form tag from:
<%= form_for #event, :html => { :class => 'form-horizontal' }, :url => url_for(:controller => 'events', :action => 'index') do |f| %>
to:
<%= form_for [:admin, #event], :html => { :class => 'form-horizontal' } do |f| %>
This will use the proper admin scope and properly generate the url, depending if the #event is persisted on the database or not.

Having issues login in with devise with a custom view

I load my custom view with this config in devise.rb
config.scoped_views = true
and then this is my app/views/users/sessions/new.html.erb
<div class="container">
<div id="login">
<%= devise_error_messages! %>
<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div class="login">
<li><%= f.label :email %> <%= f.text_field :email %></li>
<li><%= f.label :password %> <%= f.password_field :password %></li>
</div><!-- login -->
<div class="reset">
<%= f.input :email, :required => false, :autofocus => true %>
<%= f.input :password, :required => false %>
<%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %>
</div>
<div class="login">
<%= f.button :submit, "Sign in" %>
</div>
<% end %>
<%= render "links" %>
</div><!-- login -->
</div><!-- container -->
When I submit it just goes back to the page.
When I disable scoped.views it logs in fine.
I forgot you uncomment this line
config.default_scope = :user

Nested form with polymorphic association not displaying fields

I have a model with a has_one and polymorphic association like this:
class Disc < ActiveRecord::Base
has_one :item, :as => :article, :dependent => :destroy
accepts_nested_attributes_for :item
end
class Item < ActiveRecord::Base
belongs_to :article, :polymorphic => true
end
I am trying to have a nested form, but only the fields for disc are being displayed and not the ones for item. This is my form:
<%= form_for(#disc) do |d| %>
<% d.fields_for :item do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :stock %><br />
<%= f.number_field :stock, :min => 0 %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<% end %>
DISC:
<div class="field">
<%= d.label :num_discs %><br />
<%= d.number_field :num_discs %>
</div>
<div class="field">
<%= d.label :audio %><br />
<%= d.text_field :audio %>
</div>
<div class="field">
<%= d.label :subtitles %><br />
<%= d.text_field :subtitles %>
</div>
<div class="actions">
<%= d.submit %>
</div>
<% end %>
No errors are shown in the console.
Try adding an = before the first <% on the line <% d.fields_for :item do |f| %>
. I haven't used erb in awhile though so this might not be the issue