Routes error on nested controller when submitting form, databasedotcom-gem - ruby-on-rails-3

I am using the databasedotcom & databasedotcom-rails gem so that I get generate leads into Salesforce from form submissions. It was working standalone until I nested the controller under a parent, and now when I press submit I get this routes error:
No route matches [POST] "/events/516ee9a0421aa9c44e000001/leads/new"
Here is all my code:
resources :events do
resources :leads
end
class LeadsController < ApplicationController
include Databasedotcom::Rails::Controller
def new
#lead = Lead.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #lead }
end
end
def create
#lead = Lead.new(params[:lead])
#lead.event_id = params[:event_id]
#lead['OwnerId'] = '005b0000000WxqE'
#lead['IsConverted'] = false
#lead['IsUnreadByOwner'] = false
respond_to do |format|
if #event_lead.save
format.html { redirect_to #lead, :notice => 'Lead was successfully created.' }
format.json { render :json => #lead, :status => :created, :location => #lead }
else
format.html { render :action => "new" }
format.json { render :json => #lead.errors, :status => :unprocessable_entity }
end
end
end
end
<%= simple_form_for [#event, #lead], url: new_event_lead_path do |f| %>
<%= f.input :Download_Brochure__c, :check => "true", :as => :boolean, :as => :hidden %>
<%= f.input :FirstName %>
<%= f.input :LastName %>
<%= f.input :Company %>
<p>Also interested in:</p>
<%= f.input :Sponsor_Request__c, :as => :boolean, :label => "Sponsoring" %>
<%= f.input :Presenting__c, :as => :boolean, :label => "Presenting" %>
<%= f.input :Newsletter_Signup__c, :as => :boolean, :label => "Newletter" %>
<%= f.input :Privacy_Policy__c, :as => :boolean, :checked => true, :label => "Would you like to stay updated" %>
<%= f.button :submit, :label => "Submit" %>
<% end %>

The problem is in your form. With the routes as you have written them, the new_event_lead_path maps to a GET request to your LeadsController#new action. Run rake routes on the command line to confirm this.
You want to submit the form to LeadsController#create. Rails will set this up for you when you use a new instance of Lead in the expression simple_form_for [#event, #lead] provided you don't override the url. Therefore, update your form:
<%= simple_form_for [#event, #lead] do |f| %>

Related

Rails 3 Devise customization

I have an app that uses 2 type of roles: users and admins. For the users I have overwritten both the RegistrationController and PasswordsController but after adding the PasswordsController the login page for the admin(different from the user, which is custom) gives the following routing error:
Routing Error
No route matches {:action=>"create", :controller=>"password_resets", :locale=>:admin}
this is my routes.rb file:
AppName::Application.routes.draw do
devise_for :admin
namespace :backend do
root :to => "home#index"
resources :exchanges, :except => :show
resources :prices, :except => :show
resources :offers
resources :newss
resources :users, :except => :show
scope ':locale', :locale => /en|ro/ do
namespace :profile do
root :to => 'home#index'
resources :orders do
resources :items
end
end
devise_for :users, :skip => [:sessions, :registrations, :passwords]
devise_scope :user do
get 'logare' => 'registrations#logare', :as => :new_user_session
post 'logare' => 'devise/sessions#create', :as => :user_session
delete 'index' => 'devise/sessions#destroy', :as => :destroy_user_session
get 'inregistrare' => 'registrations#new', :as => :new_user_registration
post 'inregistrare' => 'registrations#create', :as => :user_registration
get 'get_city_list' => 'registrations#get_city_list', :as => :get_city_list
get 'edit' => 'registrations#edit', :as => :user_edit
post 'edit' => 'registrations#update', :as => :user_update
get 'new_password' => 'password_resets#new', :as => :password_reset
post 'new_password' => 'password_resets#create', :as => :new_password
get 'edit_password' => 'password_resets#edit', :as => :edit_password_reset
post 'edit_password' => 'password_resets#update', :as => :update_password
end
end
match '/:locale' => 'home#index'
root :to => 'Home#index'
match '*a', :to => 'errors#routing'
end
but as I was saying, this url /admin/sign_in gives the routing error mentioned.
Dunno what I'm missing. If I remove the PasswordsController, it all works again.
PS: here is the PasswordResetsController:
class PasswordResetsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
user.send_password_reset if user
redirect_to new_user_session_path, :notice => t('password_reset_notice')
end
def edit
#user = User.find_by_reset_password_token!(params[:reset_token])
end
def update
#errors_user = nil
#user = User.find_by_reset_password_token!(params[:token])
if #user
if #user.reset_password_sent_at < 2.hours.ago
redirect_to password_reset_path, :alert => t('password_expired')
else
#user.password = params[:user][:password]
#user.password_confirmation = params[:user][:password_confirmation]
unless #user.save
#errors_user = #user.errors.full_messages.join(';<br/>') + '.'
end
unless #errors_user.nil?
#reset_token = params[:token]
render :edit
else
redirect_to new_user_session_path, :notice => t('password_change_success')
end
end
end
end
end
my login form (for users, for admin I use the standard devise one):
<div class='signin-container'>
<%= form_for(:user, :url => user_session_path(#user)) do |f| %>
<%= f.label :email, 'Email:', :id => 'username_label', :class => 'general-signin' %>
<%= f.text_field :email, :id => 'username-field', :class => 'general-signin' %>
<%= f.label :password, t('inregistrare.parola'), :id => 'pass_label', :class => 'general-signin' %>
<%= f.password_field :password, :size => 23, :id => 'pass-field', :class => 'general-signin' %>
<%= f.check_box :remember_me, :id => 'remember' %>
<%= f.label :remember_me, :id => 'remember-label' %>
<%= f.submit "", :class => 'general-signin', :id => 'buton-signin' %>
<% end %>
<a href=<%= new_user_registration_path %>><span class='creeaza'><%= t('logare.creeaza') %></span></a>
<a href=<%= password_reset_path %>><span class='forgot-pass'><%= t('forgot_password') %></span></a>
</div>
figured it out ... had to generate the devise views and edit shared/_links.erb
<%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
became
<%= link_to "Forgot your password?", new_password_path(:locale => I18n.locale) %><br />

Can't mass-assign protected attributes: asset

I followed the tutorial screencast over here: http://www.emersonlackey.com/article/rails-paperclip-multiple-file-uploads. I want my model have multiple pictures upload show up.
I have examined carefully every steps, the most common issue is forget to add assets_attributes to attr_accessible, I have done that. Another issues might bbe forgot to add ID to asset model, i done that too. However, I still have trouble understanding why it happen.
Can't mass-assign protected attributes: asset in app/controllers/posts_controller.rb:24:in `update'
I have already add list of all attributes for a Post to post model. Like:
class Post < ActiveRecord::Base
attr_accessible :name, :content, :assets_attributes
validates :user_id, presence: true
belongs_to :user
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
default_scope order: 'posts.created_at DESC'
end
Here is the post_controller.rb file:
def edit
#post = Post.find(params[:id])
5.times { #post.assets.build }
end
def update
#post = Post.find(params[:id])
if #post.update_attributes(params[:post])
redirect_to #post, :notice => "Post has been updated."
end
def create
post = current_user.posts.build(params[:post])
if post.save
flash[:success] = "post created success!"
redirect_to #post
else
#feed_items = []
flash[:failure] = "post created fail!"
redirect_to root_path
end
end
def new
#post = current_user.posts.new #if signed_in?
5.times { #post.assets.build }
end
Here is the template file:
<%= simple_form_for(#post, :html => {:multipart => true}) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :content %>
<%= f.text_field :content %>
<%= f.simple_fields_for :assets, :html => { :multipart => true } do |asset_fields| %>
<% if asset_fields.object.new_record? %>
<P><%= asset_fields.file_field :asset %> </P>
<% end %>
<% end %>
<%= f.simple_fields_for :assets, :html => { :multipart => true } do |asset_fields| %>
<% unless asset_fields.object.new_record? %>
<P><%= link_to image_tag(asset_fields.object.asset.url(:thumb)), asset_fields.objects.asset.url(:original) %>
<%= asset_fields.check_box :_destroy %></P>
<% end %>
<% end %>
Below is asset.rb:
class Asset < ActiveRecord::Base
belongs_to :post
has_attached_file :asset,
:style => { :large => "640x480", :medium => "300x300", :thumb => "100x100"} ,
:path => ":rails_root/public/system/posts/images/:id/:style/:filename",
:url => "/system/posts/images/:id/:style/:filename"
end
Can someone give me some hint ? Thanks a lot!
Your Asset model needs to have attr_accessible on it too - specifically for the asset field.

Adding name value with link_to

I'm using a link_to to create an object in my Rails 3 app. Searching has given me the proper way to use link_to with the :post method, but I'm wondering if using link_to to pass in a name value for my object as well. Here is my link:
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :method => :post}, :class => "button white" %>
My profiles_controller.rb:
def create_inside
#todo = Insidetodo.new
#todo.save!
if #todo.save
redirect_to #profile.todo, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
My todo.rb model:
class Todo < ActiveRecord::Base
has_and_belongs_to_many :profiles
validates :name, :presence => true
end
Is there a way to add in :name => "#{#user.profile.todotext}" to the link_to so that it passes and saves? I don't know if it's creating properly because at the moment when I click a link_to I get a validation error - Validation failed: Name can't be blank.
For passing name in the link_to
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :name => "#{#user.profile.todotext}", :method => :post}, :class => "button white" %>
and the controller must be
def create_inside
#todo = Insidetodo.new(:name => params[:name])
if #todo.save
redirect_to #todo.goal, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
But the link_to will pass the name parameter in url only(like Todo text).
If you want the name not to be sent in the url, you might want to use a form and use a submit button instead of the link as it is representing an action and not a link.
<%= form_tag(:controller => "profile", :action => "create_profile") do -%>
<%= hidden_field_tag :name, #user.profile.todotext %>
<%= submit_tag "Todo Text" %>
<% end -%>

Rails 3 Route error - "No Route Matches"

I've been reading over this resource as well as this post to try to understand Routes more (currently learning programming/Rails by doing) but am wondering how I can fix the error I'm getting, which is No route matches {:controller=>"profiles", :action=>"show"}.
I get the error working my way through a Rails 3 sign-up process using nested model forms. The sign-up process, as follows:
user = User.new
user.email = ""
user.password = ""
user.profile = Profile.new
user.profile.save
user.save
The sign-up process starts at the homepage with the following form:
<%= form_for :user, :url => signup_path, :html => {:id => 'homepage'} do |f| %>
<div>
...
</div>
<%= f.fields_for :profile do |f| %>
<% end %>
<% end %>
Then the process goes to fill in the profile, then redirect to the new User's profile after this form is completed:
<%= form_for :profile, :html => { :multipart => true } do |f| %>
<div>
...
</div>
<%= f.fields_for :user do |f| %>
<% end %>
<% end %>
I have accepts_nested_attributes_for :user and :profile in their respective models.
My rails server it gives me a bit more detail:
ActionController::RoutingError (No route matches {:controller=>"profile.save", :action=>"show"}):
app/controllers/profiles_controller.rb:15:in `create'
So in my ProfilesController in 'create':
def create
#profile = Profile.new(params[:profile])
if #profile.save
redirect_to profile_path, :notice => 'User successfully added.'
else
render :action => 'new'
end
end
Seems clear that the issue is in profile_path, so my Routes.rb:
post "/signup" => "profiles#create", :as => "signup"
match "skip/signup", :to => "info#signupskip"
match "skip/profiles/new", :to => "profiles#newskip"
root :to => "users#create"
Can anyone help shed light on what I'm doing wrong/missing in my Routes.rb file?
The redirect path should contain the specific profile to redirect to:
if #profile.save
redirect_to profile_path(#profile), :notice => 'User successfully added.'
else
.....
Also the routes should include this line:
get "/profiles/:id" => "profiles#show", as => "profile"

how do specfiy :with attribute in rails 3 link_to

This is link_to_remote code for rails 2.3.8
link_to_remote "Click here",
:update=>'flowtracker',
:with=>"'topcount='+$('topcount').value,
:url=>{
:controller => 'sessions',
:action => 'session_for_tracker',
:layout=>1
}
link_to_remote deprecated in rails 3
so how can i specify the :with attribute in link_to rails 3
And i'm using prototype.js
Use link_to helper with :remote => true option and do whatever you want in your js view file to your action.
EXAMPLE
in view:
<%= link_to 'ajax call', :controller => "sessions", :action => "session_for_tracker", :remote => true %>
in controller:
def session_for_tracker
#topcount = 1000; #get actual value here
respond_to do |format|
format.js
end
end
in js view for action:
page << %|
$('flowtracker').update("#{#topcount}");
|