What makes the data prepopulate a field if a Form Fails? - ruby-on-rails

I made this form almost entirely from scratch and AJAX which is why it's not working, I'm assuming.
But I instantiate my form in my controller as so..
def skip_to_end
#card_signup ||= CardSignup.new(params[:card_signup])
respond_to do |wants|
wants.html { redirect_to new_card_signup_path }
wants.json { render :json => { :html => (render_to_string :partial => '/card_signups/new_form') } }
end
end
The form looks like this :
- form_for #card_signup do |f|
= f.text_field :first_name, :style => "width: 166px;", :value => "first name", :rel => "first name"
%div{:class => 'error_message'}
....
And when I try and save it halfway complete, it goes to my create action :
def create
#card_signup = current_user.build_card_signup(params[:card_signup])
if #card_signup.valid?
respond_to do |wants|
#wants.html { redirect_to disclaimer_card_signup_path, :locals => { :card_signup => #card_signup } }
wants.json { render :json => { :html => (render_to_string :partial => 'disclaimer') } }
end
else
respond_to do |wants|
#wants.html { redirect_to new_card_signup_path }
wants.json { render :json => {:errors => #card_signup.errors, :html => (render_to_string :partial => '/card_signups/new_form') } }
end
end
end
It then fails, and returns back to the original form, but with all the forms cleared.
How can I make it so that the fields are at the very least prepopulated with what they had in them previously?

Related

Want to have a partial update with ajax

I have a form:
<% form_tag({:controller => "/faq", :action => 'search_ajax'}, :update => "help_results", remote: true) do %>
that is going to the search_ajax action which is supposed to update the help_results div. That action is supposed to render a partial at the end, but I for sure am having syntax issues:
def search_ajax
#categories = HelpCategory.find(:all)
if params[:query].not_blank? && params[:query] != "Search for help about..."
#query = params[:query]
#terms = params[:query].split.map {|t| "%#{t.downcase}%" }
options = {
:allow_partial_match => true,
:order => 'fulltext_match_score DESC',
}
#results = SearchableText.search_model(HelpItem, #query, options).uniq
#results = {"Search Results" => #results} if !#results.blank?
#complicated_query = HelpItem.is_complicated_query?(#query)
#search_included = true
else
#results = HelpItem.all.group_by {|item| item.category.name rescue "Misc"}
#search_included = false
end
render :partial => "results"
respond_to do |format|
format.js do
render :partial => 'results'
end
format.html do
render :partial => 'results'
end
end
end
Some of the respond_to area is commented out. I am getting the error message:
ActionController::DoubleRenderError in FaqController#search_ajax
I know that if I add the remote => true helper into my form that the controller action needs to respond to js. I'm pretty sure this is a syntax error. The respond_to also comes at the end of my controller action.
Remove the
render :partial => "results"
Above the respond_to block

rails render is not working in create action

In my controller I coded like this
class CompanyUploadRequestsController < ApiController
def index
respond_to do |format|
format.html { render action: "index" }
end
end
def create
puts params
respond_to do |format|
format.html { render action: "index" }
end
end
def new
end
end
and in my view new.html.haml file
- page_title("Upload Company")
%h1 Upload Company
%hr
#upload-form
= simple_form_for(#company_upload, :as => :company_update, :url => company_upload_requests_path(#company_upload), :html => { :class => 'file-style'}) do |f|
= f.error_notification
.form-inputs
= f.input :requestname, :label => false, :id => "request_name_input"
= f.input :file,:as => :file, :label => false, :id => "file_select_input"
.form-actions
!= link_to 'Cancel', company_upload_requests_path, :class => 'btn'
= f.button :submit, 'Upload', :class => 'btn-primary'
In my index.html.haml file I have this
- page_title("Upload Company")
%h1 Company index
= link_to("Upload File", new_company_upload_request_path, :class => "btn btn-primary pull-right")
The problem is when I click upload button its not render to index page from create
Here I got the log like this
Processing by CompanyUploadRequestsController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"oygIP62E4GHefhN9OnvB3sKhddIb4CN/izfvF5GQtuI=", "company_update"=>{"requestname"=>""}, "commit"=>"Upload"}
Rendered company_upload_requests/create.html.haml within layouts/application (9.8ms)
How can I render to index action and view index file content.
Use like this.
def index
#company_uploads = ModelName.all
respond_to do |format|
format.html
end
end
No need to render index action in index response.
def create
puts params
respond_to do |format|
format.html { render "index" }
end
end
Change the render in your create method to redirect_to
def create
puts params
respond_to do |format|
format.html { redirect_to action: "index" }
end
end
For more explanation on render vs redirect_to, see this SO Question or this.

Rails how can I pass collection to a partial view, that is rendered from *.js.erb file

When I do this in my update.js.erb from the RewardsController:
$('div#rewards_list').html("<%=
escape_javascript(render :partial => 'shared/rewards',
:collection => #rewards,
:as => reward,
:locals => { :user => #user }
)
%>");
But the log says:
ActionView::Template::Error (undefined local variable or method `reward' for
#<#<Class:0xbb395d8>:0xbb3774c>):
In my 'update' action method:
def update
#user = User.find(params[:user_id])
#reward = #user.rewards.find(params[:id])
respond_to do |format|
if #reward.update_attributes(params[:reward])
#rewards = #user.rewards
format.html { redirect_to #user }
format.js
else
flash[:error] = "There is an error while updating the reward, please try again!"
format.html { redirect_to #user }
end
end
end
The :as option should be given a symbol, not a variable (and especially not a variable that doesn't exist), perhaps you mean this:
$('div#rewards_list').html("<%= escape_javascript(render :partial => 'shared/rewards', :collection => #rewards, :as => :reward, :locals => {:user => #user}) %>");
I just changed :as => reward to :as => :reward. See the Layouts and Rendering in Rails guide for details.

When submitting a form with a fields_for part, how can I assign the new id to the fields_for model

I have a form that handles 2 models, Vehiculo and Poliza. This is how I have them set up right now:
class Vehiculo < ActiveRecord::Base
has_one :poliza
end
class Poliza < ActiveRecord::Base
belongs_to :vehiculo
end
The create method on Vehiculo looks like this:
def create
#vehiculo = Vehiculo.new(params[:vehiculo])
#polizadeseguro = Polizadeseguro.new(params[:poliza])
respond_to do |format|
if #vehiculo.save #&& #poliza.save
format.html { redirect_to(#vehiculo, :notice => 'Vehiculo was successfully created.') }
format.xml { render :xml => #vehiculo, :status => :created, :location => #vehiculo }
else
format.html { render :action => "new" }
format.xml { render :xml => #vehiculo.errors, :status => :unprocessable_entity }
end
end
The form on /vehiculos/new has a #fields_for part with the fields from poliza. When I submit the form, it is saving all the fields, but it is not assigning the just created id from vehiculo, to vehiculo_id on the Polizas table. After reading many questions about this online, It seems that it should save it "automagically" based on the relationships on the model. Is this true? If so, why isn't it working? If not, what do I need to add to the create method so I resolve this?
Thanks!
Update:
After updating the create method with json as output as suggested here is what I get:
{
"utf8"=>"✓",
"authenticity_token"=>"tEhNC4J17h+KvNgXv1LLkVyufQwU2uAT18P7msQxiqA=",
"vehiculo"=>{
"marca_id"=>"2",
"modelo_id"=>"4",
"color"=>"Blanco",
"ano"=>"2011",
"chassis"=>"123456789",
"placa"=>"G123456",
"cliente_id"=>"1",
"entaller"=>"0",
"vip"=>"0"
},
"poliza"=>{
"compania"=>"Comp1",
"numeropoliza"=>"736458",
"vencimiento(1i)"=>"2011",
"vencimiento(2i)"=>"9",
"vencimiento(3i)"=>"21"
}
}
That's the output, so at least it is getting the fields from the form, but it is not inserting them to the polizas table.
You need to make sure that your parent model accepts nested attributes for the child model:
class Vehiculo < ActiveRecord::Base
has_one :poliza
accepts_nested_attributes_for :poliza
end
Assuming your form is set up correctly, your params will look something like this:
params = {
:vehiculo => {
:field => "value",
:another_field => "value",
:poliza => {
:poliza_field => "poliza value"
}
}
}
So all you should need in your controller is:
def create
#vehiculo = Vehiculo.new(params[:vehiculo])
respond_to do |format|
if #vehiculo.save #&& #poliza.save
format.html { redirect_to(#vehiculo, :notice => 'Vehiculo was successfully created.') }
format.xml { render :xml => #vehiculo, :status => :created, :location => #vehiculo }
else
format.html { render :action => "new" }
format.xml { render :xml => #vehiculo.errors, :status => :unprocessable_entity }
end
end
end
[Update]
Here's what you'll need to have to have this all work.
As mentioned above, you need accepts_nested_attributes_for.
Next, make sure your new action is building the child.
class VehiculosController < ApplicationController
def new
#vehiculo = Vehiculo.new
#vehiculo.build_poliza
end
def create
vehiculo = Vehiculo.new(params[:vehiculo])
if vehiculo.save
redirect_to root_path, :notice => "Success"
else
redirect_to root_path, :alert => "Failure"
end
end
end
Finally, in your view, reference the child model using fields_for :child_model, as such:
<%= form_for #vehiculo do |f| %>
<p>Whatever Field: <%= f.text_field :whatever %></p>
<%= f.fields_for :poliza do |p| %>
<p>Polizo Field: <%= p.text_field :something %></p>
<% end %>
<% end %>

Confused as to which Prototype helper to use (updated)

This is a continuation of Confused as to which Prototype helper to use. My code has been updated to reflect other user's suggestions:
(model) message.rb:
class Message < ActiveRecord::Base
after_create :destroy_old_messages
def old_messages
messages = Message.all(:order => 'updated_at DESC')
if messages.size >= 24
return messages[24..-1]
else
return []
end
end
protected # works without protected
def destroy_old_messages
messages = Message.all(:order => 'updated_at DESC')
messages[24..-1].each {|p| p.destroy } if messages.size >= 24
end
end
(view) index.html.erb:
<div id="messages">
<%= render :partial => #messages %>
</div>
<%= render :partial => "message_form" %>
(view) _message.html.erb:
<% div_for message do %>
<%= h message.created_at.strftime("%X") %> - <%= h message.author %><%= h message.message %>
<% end %>
(view) _message_form.html.erb:
<% remote_form_for :message, :url => { :action => "create" }, :html => { :id => 'message_form'} do |f| %>
<%= f.text_area :message, :size => "44x3" %><br />
<%= submit_to_remote 'submit_btn', 'submit', :url => { :action => 'create' } %><br />
<% end %>
(view) create.rjs:
page.insert_html :top, :messages, :partial => #message
page[#message].visual_effect :grow
page[:message_form].reset
flash[:notice]
flash.discard
# #old_messages.each do |m|
# page.remove(m.id)
# end
(controller) messages_controller.rb:
class MessagesController < ApplicationController
def index
#messages = Message.all(:order => "created_at DESC")
respond_to do |format|
format.html
format.js
end
end
def new
#message = Message.new
respond_to do |format|
format.html
end
end
def create
#message = Message.new(params[:message])
# #old_messages = Message.old_messages
respond_to do |format|
if #message.save
flash[:notice] = 'message created.'
format.html { redirect_to(messages_url) }
format.js
else
format.html { render :action => "new" }
end
end
end
def update
#message = Message.find(params[:id])
respond_to do |format|
if #message.update_attributes(params[:message])
flash[:notice] = 'message updated.'
format.html { redirect_to(messages_url) }
format.js
else
format.html { render :action => "edit" }
end
end
end
def destroy
#message = Message.find(params[:id])
#message.destroy
respond_to do |format|
format.html { redirect_to(messages_url) }
format.js
end
end
end
With the exception of the old_messages method in the model, all of the commented code were recommended changes from the previous post to make this work. But as soon as I uncomment the last three lines from create.rjs and #old_messages = Message.old_messages from the controller, I can't even submit messages with the message_form partial. Can anyone see what's wrong here? I'm just trying to create a basic app to help further my understanding of rails and rjs. I would greatly appreciate any suggestions or corrections you have to share, thank you for reading my post.
it's not what you're asking for, but i have a suggestion...
to get the older messages you can use named_scope.
in your case, (if i understood what you want) i think it would be something like:
# model
named_scope :limit, lambda { |num| { :limit => num } }
named_scope :order, lambda { |ord| { :order => ord } }
and
#controller
Message.order("updated_at DESC").limit(24)
the problem is that old_messages is an instance method, and you're calling from a class.
if you do
def self.old_messages
# ...
end
it's now a class method.
this blog has a good explanation about class and instance methods.

Resources