Answer the question
In order to leave comments, you need to log in
How to render partial from controller?
Product
.... Category
.... Name
.... Price
, etc.
New product:
display a form with fields:
Category
Name
Price
, etc.
The user selects a category. Depending on the category, category-specific fields are added to the form, such as: diagonal, resolution, interfaces,
or
power, grill function,
or
inkjet / laser printing, print speed,
or
etc.
I would like to make this feature through ajax, so that html is prepared on the server for insertion into an existing form.
I use simple_form in the project.
How should the controller method that sends the Form Piece to the browser approximately look like.
Here is my controller code:
def category_details
simple_form_for @item do |f|
render partial: "/items/details_edit", locals: {item_form: f}
end
end
undefined method `simple_form_for'
<form ...></form>
? Answer the question
In order to leave comments, you need to log in
Total worked out.
Controller. Action to return detail fields
def category_details
@item = Item.new
@item.category = params[:id]
render 'details_form', layout: false
end
<%= simple_form_for @adv do |f| %>
логика, которая в зависимости от @item.category, генерит нужные поля с помощью хелперов simple_form
<% end %>
<i>JavaScript:</i>
$('#поле с выбором категории').change(function(){
$('#item_details').empty();
$.get('/cabinet/category/'+$(this).val()+'/details_form', {},
function(data){
$(data).children("[type != hidden]").appendTo('#item_details');
}
);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question