I am atempting to dinamically create form elements given a certain AJAX request.
This is my setup:
View:
<%= link_to 'Next', check_unique_id_students_path, :remote => true %>
<div id="guardian_student_details"></div>
Controller:
def check_unique_id
#student = Student.new
#this_form = ActionView::Helpers::FormBuilder.new(:student, #student, #template, {}, proc{})
end
JS:
jQuery("#guardian_student_details").html("<%=escape_javascript(render :partial => "student_details", :locals => { :s => #this_form }) %>");
Partial:
<% puts s.text_field :first_name %>
<% puts s.field_helpers %>
For debugging purposes i placed the following lines at the very beginning of my partial:
<% puts s.class.to_s %>
<% puts s.object.to_s %>
This prints out :
ActionView::Helpers::FormBuilder
Student
This should work. However rails is giving the following error:
ActionView::Template::Error (undefined method `text_field' for nil:NilClass):
1: <% puts s.class.to_s %>
2: <p>
3: <%= s.text_field :first_name, :class => 'text_input is_empty' %>
4: <%= s.label :first_name %><strong>*</strong>
5: </p>
6:
app/views/students/_student_details.html.erb:3:in _app_views_students__student_details_html_erb__2485891544130782916_2214680440'
app/views/students/check_unique_id.js.erb:2:in_app_views_students_check_unique_id_js_erb__3504800328150418937_2214933160'
Which implies that "s" is NIL something I verified just 2 lines before. Does anybody have any ideas? i dont know if this has something to do with the "#template" variable initialized in the controller. Which i played around with and accepts practically anything and if printed is nil.
Any help would be appreciated. Thanks
Final note:
I tried to implement this: AJAX update of accepts_nested_attributes_for partials
For anyone needing to build a form builder in the controller, view_context still works there. Using Rails 4.1.4:
#object = Object.new
#f = ActionView::Helpers::FormBuilder.new(:object, #object, view_context, {})
In the view, I've found that 'view_context' does not work in Rails 3.1. Instead try 'self' when creating a FormBuilder object.
s = ActionView::Helpers::FormBuilder.new(:student, #student, self, {}, proc{})
Try this in a console :
s = ActionView::Helpers::FormBuilder.new(:student, #student, #template, {}, proc{})
s.text_field :first_name
You will have the same error. I think the problem come from your creation of the form_builder object, even if I don't know the exact mistake...
Your solution seems to me to be a little much complex. You can try this solution :
#html.erb
<% form_for #student do |f| %>
<div id='guardian_student_details' class='hide-this-block'>
<%= render :partial => "student_details", :locals => { :s => f }) %>
</div>
<% end %>
#js
jQuery("#guardian_student_details").show();
Generally, I prefer keep javascript and ruby separated.
Related
I am trying to generate dynamic CSS ids for javascript purposes, and the following code is giving me an error:
<%= form_for #item, html: { multipart: true } do |f| %>
<%= f.fields_for :item_images do |builder| %>
<%= image_tag builder.object.image.url(:large), :class => "cropbox", 'data-id' => builder.object.id %>
<% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
<%= builder.text_field attribute, :id => attribute + builder.object.id %>
<% end %>
<% end %>
<% end %>
I know I'm not concatenating attribute and builder.object.id properly, but I've tried everything with no luck. I get this error message:
undefined method `+' for :crop_x:Symbol
Appreciate any help, thanks!
What is your expected result?
Can you p out what '''attribute''' is within your expression?
Also, have you tried .concat instead of '+'?
Away from my desk, but my suggestion is something like:
Example:
attribute.concat(builder.object.id)
Maybe need to convert attribute.to_i or .to_s into a responsive type or create a different dynamic CSS id solution
I have this form (simplified)
<%= form_for #client do |client| %>
<%= render "some_partial", foo: 10, bar: 20 %>
<% end %>
I also try with: <%= render :partial => "some_partial", :locals => {foo: 10, bar: 20} %>
And in some_partial:
<%= foo %>
<%= bar %>
But, the variables are not available on partial:
undefined local variable or method `foo' for #<#<Class:0x007fb35027bbd0>:0x007fb350536e60>
If I put render out side of form_tag works fine
Thanks in advance
UPDATE1: At the end of form, I have this:
<%= link_to_add_association 'Add', client, :client_addresses, class: "ui mini green button add_client_address" %>
If I remove this part, works fine...
You have to mention parameters as locals, like this.
<%= render :partial => "some_partial", :locals => {foo: 10, bar: 20} %>
Is the "some_partial" the same :client_addresses you are trying to load with the link_to_add_association?
I believe that the error message you are experiencing is coming from within the link_to_add_association
Seems you are using the Cocoon gem. In order to pass locals into the partial that Cocoon uses (:client_addresses in your case) you need to pass the parameter render_options: { locals: {foo: bar} for the link_to_add_association
I have form which is built like this:
<%= form_for #location do |f| %>
<%= f.fields_for :product_dates do |d| %>
<%= d.fields_for :products |p| %>
<%= p.text_field :tag_list,"data-pre" => #product.tags.map(&:attributes).to_json %>
Now when i go to the page i get an error when using the line: "data-pre" => #product.tags.map(&:attributes).to_json which is undefined method tags for nil:NilClass but everything is fine when i take it away. This some type of TokenInput bug? Anyone else had to deal with this?
ProductsController:
def new
#location = Location.new
product_date = #location.product_dates.build
product_date.products.build
end
You simply didn't set your #product variable => it's nil.
You should show your controller
EDIT:
replace:
<%= p.text_field :tag_list,"data-pre" => #product.tags.map(&:attributes).to_json %>
with:
<%= p.text_field :tag_list,"data-pre" => p.object.tags.map(&:attributes).to_json %>
This should work for edit as well.
It's really good sense here: you can't invoke something you didn't set.
I have a problem I can not resolve on a form
Here's my view:
<h1>create manager </h1>
<% form_tag :action => 'create_manager' do %>
<%= text_area :user, :nom %><br/>
<%= date_select :user, :date_embauche %>
<%= submit_tag "Submit" %>
<% end %>
and here is my controller:
def create_manager
tmp = params[:nom]
p(tmp)
render :partial => "adminpartial"
end
The problème is that params[:nom] return alltime nil.
I think i'm not using correctly the params variable.
Does anyone have an idea about this?
Did you look at the params passed in your logs?
I guess you might find something in params[:user][:nom]as explained here.
BTW, did you carefully read this?
Okay so I am quite new to Rails and am trying to do the following without success:
I have an Object (from my Active Record) containing a project, which contains n sub-projects, which contain n tasks. Now for each of these I want a partial view.
So I render from the project view the sub-project with the following code:
<%= render(:partial => 'subproject', :collection => #project.sub_projects) %>
Within my sub-project partial view called _subproject.rhtml (adding the code to a good ol Rails 1.2.3 project), so I can access the data like this:
<%= subproject.name %>
That will print out the name alright but when I try to generate a textfield this won't work:
<%= text_field 'subproject', 'name' %>
But this will:
<%= text_field 'subproject', 'name', :value => subproject.name %>
What am I doing wrong?
Edit: Changed title due to my problem is not passing the value but displaying it within a form field.
Edit2: As requested my controller code:
#project = Project.find(params[:id])
You can write this:
<%= render(:partial => 'subproject', :collection => #project.sub_projects) %>
as
<%= render :partial => #project.sub_projects %>
This will render every sub project with the sub_projects/_sub_project.html.erb partial. A little shortcut.
This:
<%= text_field 'subproject', 'name' %>
Says create a text_field called: subproject[name], but doesn't give it a value. You need to pass the value you want to set (the code that works).
The more idiomatic way of doing this now is with form_for:
<% form_for #subproject do |f| %>
<%= f.text_field :name %>
<% end %>
Or if you're using formtastic (https://github.com/justinfrench/formtastic), which is fantastic, you'd write:
<% semantic_form_for #subproject do |f| %>
<%= f.input :name %>
<% end %>
I hope this helps!