I want to change form common name in rendering side
_from.html.haml
..
= f.simple_fields_for Image.new do |form|
= render 'avatar_fields', f: form
..
_avatar_fields.html.haml
..
= f.hidden_field :imageable_type
..
This is rendering like
<input id="product_image_imageable_type" name="product[image][imageable_type]" type="hidden">
But i want to render like this
<input id="product_logo_attributes_imageable_type" name="product[logo_attributes][imageable_type]" type="hidden">
I don't want to edit my '_avatar_fields.html.haml' screen. Because it's common html.
Any suggestion please..?
you can do some thing like
...
= f.hidden_field :imageable_type, input_html: {id: 'product_logo_attributes_imageable_type'}
...
I hope that this helps you
Related
I am working with a nested model a question has multiple answers and only one can be marked as correct how can i validate to check that only one question was marked as correct. The correct is a boolean field.
#question model
validate :one_correct_answers
def one_correct_answers
if self.choices.correct_choices > 1
errors.add(:base, "please select only one correct answer")
end
end
In Question model
class Question
has_many :choices
accepts_nested_attributes_for :choices, :reject_if => ->(choice){ choice[:value].blank? }
validate :only_one_correct_answer
private
def only_one_correct_answer
unless (choices.select{ |choice| choice.correct }.size == 1)
errors.add(:choices, "You must provide only 1 correct answer")
end
end
end
In Form HTML
<input name="question[choices_attributes][0][correct]" type="checkbox">
<input name="question[choices_attributes][1][correct]" type="checkbox">
<input name="question[choices_attributes][2][correct]" type="checkbox">
<input name="question[choices_attributes][3][correct]" type="checkbox"> ... till n
and in QuestionsController
#question = Question.new(params[:question])
#question.valid? => will automatically call Question#only_one_correct_answer and add errors,if any.
I hope, this will help you. :)
First, your checkboxes should look like this:
<input id="something_" name="something[]" type="checkbox" value="<%= some_id %>">
This way, when you submit the form, the params should look like and array of the checked checkboxes:
params: something => [1, 2]
Then, on your controller you set the variable correct_choices to this array, and validate with your custom validator.
I have in a form (form_tag) several checkboxes like this:
<%=check_box_tag 'model_name[column_name]', 1, (#data.model_name.column_name == 1 ? true : false)%>
And updating them like:
variable = ModelName.find(params[:id])
variable.update_attributes(params[:model_name])
This works only in a moment, when I check some checkboxes - send them and they will be saved. That's fine.
But when I uncheck all checkboxes - send form - so nothing happend, in the DB table will not set the value 0 in the columns...
Could you give me any tip, how to fix it?
Thank you in advance
This happens because an unchecked checkbox will not send any value to the server. To circumvent this Rails provides the check_box helper, which generates code like this:
<input type="hidden" name="model[attr]" value="0" />
<input type="checkbox" name="model[attr]" value="1" />
Alternatively, insert a hidden field with hidden_field_tag:
<%= hidden_field_tag 'model_name[column_name]', '0' %>
<%= check_box_tag 'model_name[column_name]', 1, (#data.model_name.column_name == 1 ? true : false) %>
Like in title - I've got a tag (no model) based form (form_tag) and I want after submit obtain values entered in fields of that form - how can I do it?
When a form is submitted, such as the one below:
<% form_tag do %>
<label for="first_name">First Name:</label>
<%= text_field_tag :first_name %>
<% end %>
a params hash is set so you can easily access its values in your controller, like so:
value = params[:first_name]
Just use the params hash in the controller, like so:
def update
field_value = params[:field_name_here]
end
I am using a helper method from ryan bates railscasts on ancestry to display nested messages(code below works perfectly).
def nested_messages(messages)
messages.map do |message, sub_messages|
render(message) + content_tag(:div, nested_messages(sub_messages), :class => "nested_messages")
end.join.html_safe
end
The above bit of code nests the individual divs in a tree like structure. I would like to make this into an unordered list, so what i have done is this:
def nested_messages(messages)
messages.map do |message, sub_messages|
content_tag(:ul, :class => "") do
render(message)
content_tag(:li, :class => "nested_messages") do
nested_messages(sub_messages)
end
end
end.join.html_safe
end
The generated html looks fine, however the list items contain no values. Am i doing something wrong?
UPDATE
I would like the generated html to look like this:
<ul>
<li>Main Message</li> <!-- first message -->
<li>
<b>Message 1</b>
<ul>
<li>Message 1 subchild 1</li>
<li>Message 1 subchild 2</li>
</ul>
</li>
</ul>
UPDATE 2
I have changed it to this and it works, thanks to Dave:
def nested_messages(messages)
messages.map do |message, sub_messages|
#render(message) + content_tag(:div, sub_messages, :class => "nested_messages")
content_tag(:ul, :class => "") do
content_tag(:li, :class => "nested_messages") do
render(message) + nested_messages(sub_messages)
end
end
end.join.html_safe
end
You create a ul tag, then render the message. If you do that, what will your HTML look like?
Things inside a ul should be in a nested li: you just render the message.
You need to put it in an li tag so the unordered list has valid content.
Hi i am using the date picker jquery ui in combination with rails 3.1. The date picker looks brilliant, only the date isn't stored in the database? Only sometimes...? So that's a difficult error.
This is my .js file:
$(function() {
$("#question_deadline").datepicker({ duration: 'fast', maxDate: '+2m', minDate: 'now', showOn: "button", buttonImage: "calendar.gif", buttonImageOnly: true });
$("#question_deadline").datepicker("option", "showAnim", "drop");
$("#question_deadline").datepicker("option", "dateFormat", "DD, d MM, yy");
});
In my controller there's just plain rails script:
def create
#question = Question.new(params[:question])
if #question.save
redirect_to questions_path, :notice => "Successfully created question."
else
setup_questions
render :index
end
end
In views file _form.html.erb i use a text_field to display the date:
<div class="field">
<%= f.label :content, "Question" %><br />
<%= f.text_field :content, :placeholder => "type your question here.." %>
<%= f.text_field :deadline %><br />
</div>
Are there people who have experience with datepiacker jquery ui and rails, the ryan bates episode, didn't solve it, i think that was written in rails 2.3?
Regards,
Thijs
First, you need to show us the view where you have the datepicker element. If it's like this:
<input type="text" name="question_deadline" id="question_deadline" />
When you submit this form, the parameters you receive in your controller (in the method "create") is called question_deadline. So in that create method you should first write:
if params[:question_deadline] != ""
params[:question][:question_deadline] = params[:question_deadline]
end
#add a else if this date field is compulsory in the database
This step is important because the create method will read stuff from params[:question][:question_deadline] not from params[:question_deadline] which is returned from the view.
Thus params[:question][:question_deadline] is empty when you do #question.save
To display the date, you also need to show us the controller "show" method that should be something like:
#question = Question.find(params[:id]) #or any sql request that returns info about a question.
Then in the view you can retrieve it simply with:
<%= #question.question_deadline%>
Maybe with more code from you controller and view I can elaborate on that.
I think, Rails/Ruby is not able to parse a date in this format:
$("#question_deadline").datepicker("option", "dateFormat", "DD, d MM, yy");
// full day name, day (w/o leading zero), full month name, 4-digit year
In your controller, you might want to add a line such as
def create/update
...
#question.deadline = DateTime.strptime(params[:question][:deadline], '%A, %d %B, %Y')
# assuming my jquery-to-ruby format-mapping is adequate ;-)
if #question.save
...
end
Beware, that this code easily breaks on malformed date strings.
If you don't want to change the format to, e.g. 'yy-mm-dd' (in Ruby-land it's '%Y-%m-%d'), you may want to populate the selected date to another HTML element using the altField option and hide the actual datepicker input field via CSS:
$("#somewhere_else").datepicker(
dateFormat: "%yy-%mm-%dd",
altField: "#question_deadline",
altFormat: "DD, d MM, yy",
...
);
<%= form_for #question do |f| %>
...
<%= text_field_tag 'somewhere_else', #question.deadline %>
<%= f.hidden_field :deadline %>
...
<% end %>
That'll work, at least for me :-)
—Dominik
The other option is to update the way ActiveSupport parses dates. This is outlined in Default Date Format in Rails (Need it to be ddmmyyyy)