how to request a fields_for partial via ajax? - ajax

i have a nested form model
<%= simple_form_for(#product, :html => { :multipart => true }) do |f| %>
<%= f.input :name %>
#... and some other
<%= f.simple_fields_for :productgroups do |builder| %>
<%= render 'form_productgroup', { :f => builder } %>
<% end %>
<% end %>
## partial-level-1: form_productgroup
<%= f.input :extrapercentage %>
#... and some other
<%= f.simple_fields_for :productmaterials do |builder| %>
<%= render 'form_productmaterial', { :f => builder } %>
<% end %>
## partial-level-2: form_productmaterial
<%= f.input :price %>
#... and some other
=> everything fine serverside, but i need to add records via ajax because each productgroup or productmaterial belongs to a specific type
now the question:
how to create a controller action which returns the new material form?
This code returns an error that f is not existing:
def new_productgroup
#product = Product.find(params[:id])
#productgroup = Productgroup.new(:product_id => #product.id, :materialgroup_id => params[:materialgroup_id])
render "form_productgroup", { :f => ???? }
end
please help me!

Related

Padrino and sequel nested attributes form

I am trying to make a form for storing data hierarchy through the associations.
My structure of database is
meta
- ID (PK)
version
- meta_id (FK of meta)
- ID (PK)
- lang (FK from lang ...)
- ....
data
- ID (FK of version)
I need to store multiple language content under one ID.
Models are
class Meta < Sequel::Model(:link_meta)
one_to_many :version
end
class Version < Sequel::Model(:link_version)
one_to_one :meta, key: :meta_id
one_to_many :data, key: :id
one_to_one :lang, key: :lang
end
class Link < Sequel::Model(:link)
plugin :timestamps, :create => :created, :update => :updated
many_to_one :version, key: :id
end
And my form for creating input is.
<% form_for :links, url(:links, :create), :class => 'form-horizontal' do |f| %>
<%= partial 'links/form', :locals => { :f => f } %>
<% end %>
And this is my partial
<% f.fields_for :version, :class => 'form-horizontal' do |cz| %>
<%= cz.hidden_field :lang, :value => Lang.select(:id).where(:lang => "cz").map(:id) %>
<% cz.fields_for :data, :class => 'form-horizontal' do |link| %>
<% error = #links.errors.key?(:url) && #links.errors[:url].count > 0 %>
<fieldset class='control-group <%= error ? 'has-error' : ''%>'>
... form content
</fieldset>
<% end %>
<% f.fields_for :version, :class => 'form-horizontal' do |en| %>
<%= en.hidden_field :lang, :value => Lang.select(:id).where(:lang => "cz").map(:id) %>
<% en.fields_for :data, :class => 'form-horizontal' do |link| %>
<% error = #links.errors.key?(:url) && #links.errors[:url].count > 0 %>
<fieldset class='control-group <%= error ? 'has-error' : ''%>'>
... another form content
</fieldset>
<% end %>
<% end %>
But it fails on
undefined method `data' for [:class, "form-horizontal"]:Array
I tried different association combinations etc, I feel lost.
Thanks
Levi

Always showing error message on the form with Ruby on Rails

I'm facing this problem how to fix this issue?
This is my problem, always showing an error message:
This is my model:
validates :first_name, :presence => true, :length => { :in => 3..20 }
validates :last_name, :presence => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, format: { with: /\A[^#\s]+#([^#.\s]+\.)+[^#.\s]+\z/ }
# validates :passkey, :confirmation => true #password_confirmation attr
validates_length_of :passkey, :presence => true, :in => 6..20, :on => :register
This is my Controller:
def register
params.permit!
#jobseekers = Jobseeker.new(params[:jobseekers])
if #jobseekers.save
redirect_to home_path
else
render "register"
end
end
This is my view:
<%= form_for :jobseekers, url: register_path(#jobseekers), method: :patch do |f| %>
<%= f.text_field :first_name, placeholder: 'First Name'%>
<%= f.text_field :last_name, placeholder: 'Last Name'%>
<%= f.text_field :email, placeholder: 'Email'%>
<%= f.password_field :passkey, placeholder: 'Password'%>
<%= f.submit "Create Account" %>
<% end %>
<% if #jobseekers.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#jobseekers.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% #jobseekers.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
I need show this error after click submit button, but now showing this error always.
Please help!
Now I'm using ruby 1.9.7 rails 4.2.5 & mysql2
The problem in your code is the controller action:
def register
params.permit!
#jobseekers = Jobseeker.new(params[:jobseekers])
if #jobseekers.save
redirect_to home_path
else
render "register"
end
end
if you take a look, then you don't just create a new object #jobseekers but try to save it right away. if #jobseekers.save will be false because the params[:jobseekers] is empty when you call the action for the first time. Since you have validations, it will automatically add errors, and your register view will be rendered with those errors. This is why usually there are 2 actions with forms- new and create.
To fix this split this into two actions
def register
#jobseekers = Jobseeker.new
end
def create_register
params.permit!
#jobseekers = Jobseeker.new(params[:jobseekers])
if #jobseekers.save
redirect_to home_path
else
render "register"
end
end
where create_register only has a post route and register has a get route (register will render the form for the first time, and then after submit the create_register will be called and render the form with errors if there are any)
This would then be your view(stating the method is not necessary in this case):
<%= form_for :jobseekers, url: create_register_path(#jobseekers) do |f| %>
<%= f.text_field :first_name, placeholder: 'First Name'%>
<%= f.text_field :last_name, placeholder: 'Last Name'%>
<%= f.text_field :email, placeholder: 'Email'%>
<%= f.password_field :passkey, placeholder: 'Password'%>
<%= f.submit "Create Account" %>
<% end %>
<% if #jobseekers.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#jobseekers.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% #jobseekers.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
and your routes.rb entries:
get '/register', to: "jobseekers#register"
post '/register', to: "jobseekers#create_register"
Another take on Kkulikovskis' answer would be:
def register
params.permit!
#jobseekers = Jobseeker.new(params[:jobseekers])
return true if request.get? # <--- skip validations if not on form submit
if #jobseekers.save
redirect_to home_path
else
render "register"
end
end
I prefer this approach when the instantiation of #jobseeker is less trivial
PS params.permit! is a path to hell, don't do it

Add extra options to link_to do

I would like to pass a parameter with this link_to helper. How do I do that?
<%= link_to ('/my_controller/my_action') do %>
<div>haha</div>
<% end %>
The original code I had used the standard text link:
<%= link_to("link text", {:controller => 'my_controller', :action => 'my_action', :id => my_id}) %>
Just like in your second example:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
My text goes here
<% end %>
If you have a path helper for this (i.e. if it's something like PostsController's show action) then you could do:
<%= link_to post_path(my_id) do %>
My text goes here
<% end %>
From my understanding you want to pass options to the link_to helper when using a block.
From the documentation at http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
This is possible with the following format
link_to(options = {}, html_options = {}) do
# name
end
To give you an example and using the code you provided:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
<div>haha</div>
<% end %>

NoHandlerError Paperclip on ruby on rails 3.2.1

I am trying to make available to upload a multiple pictures with paperclip. I am following this tutorial: http://www.emersonlackey.com/article/paperclip-with-rails-3
Unfortunately I have error:
No handler found for "apple_mac-1280x800.jpg"
Asset.rb
class Asset < ActiveRecord::Base
belongs_to :post
has_attached_file :asset, :styles => { :small => "100x100", :original => '800x600>'}
end
Post.rb
class Post < ActiveRecord::Base
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
end
Posts_controller.rb
def new
#post = Post.new
5.times { #post.assets.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: #post }
end
end
def edit
#post = Post.find(params[:id])
5.times { #post.assets.build }
end
#Post form
<% create_url = {:url=>{:action=>"create"}} if #post.new_record? %>
<% form_for #post, :html => {:multipart => true} do |t| %>
<p>
<%= t.label :title, 'Virsraksts:' %>
<%= t.text_field :title %>
</p>
<p>
<%= t.label :content, 'Teksts:' %>
<%= t.text_area :content, :class => "mceEditor"%>
</p>
<%= t.fields_for :assets do |asset_fields| %>
<% if asset_fields.object.new_record? %>
<p>
<%= asset_fields.file_field :asset %>
</p>
<% end %>
<% end %>
<%= t.submit %>
<% end %>
<%end%>
Problem solved by changing
<%= t.fields_for :assets do |asset_fields| %>
to
<%= f.fields_for :assets do |asset_fields| %>
But could someone explain why?

how does a button_to in rails request a JSON response?

Im a rails noob, so bear with me.
I have the following in new.html.erb:
<h1>New page</h1>
<%= render 'pages/form' %>
<% if #page != 1 %>
<%= button_to 'New Page', pages_path %>
<%= button_to 'Done!', :action=> 'generate' %>
<% end %>
my controller looks like this
def generate
#presentation = current_presentation
respond_to do |format|
format.json { render json: #presentation.pages}
format.html {render :text => "html"}
end
end
I want the response to be JSON, but the code goes to format.html.
How do I instruct the button_to that my requested response is JSON?
Not sure if just one of them or both, but try :remote => true and data-type:
<%= button_to "Create", :action => "create", :remote => true, :form => { "data-type" => "json" } %>
Using a path helper to build the path worked for me. eg.
<%= button_to "Create Widgit", widgits_path(format: 'json'), remote: true %>

Resources