One Model with mutiple Forms in rails - ruby-on-rails-3

I came to a situation where I need to have 2 different forms one for "new" action and other for "edit" action.
Currently my new action view have
= render 'form'
But when I try to create another form for edit
Edit action view
= render 'form2'
It doesn't work it return an error message of
ActionView::MissingTemplate in Google_categories_to_masters#edit
Showing C:/master/EDM/app/views/google_categories_to_masters/edit.html.erb where line #3 raised:
Missing partial google_categories_to_masters/form2, application/form2 with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
* "C:/master/EDM/app/views"
* "C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/kaminari-0.13.0/app/views"
* "C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/devise-1.4.7/app/views"
Can anyone teach me how to can I do in rails application.

You can use one form for both new and edit.
In new
render 'form'
and in edit also
render 'form'

Related

Passing a custom model to a Partial in Razor Pages

I have a partial page which is passed a list of Events (where an event is a custom Model). This works fine:
#model IEnumerable<Entry>
#using MySite.Models
Within the partial page I iterate through the list of Entry's in the model:
#if (Model.Count() > 0)
{
foreach (var entry in Model)
{
...
This works fine as well. However, within this foreach loop I want to send the data of the entry to another partial view:
#await Html.PartialAsync("/Pages/Shared/ShareMedia/shareMedia.cshtml", #entry)
Within this partial I have declared the following:
#model Entry
However this seems t return an error:
error CS0118: 'Entry' is a namespace but is used like a type
If I then try to add the full path of the model
#model MySite.Models.Entry
I get the following error when trying to load the partial:
Uncaught ReferenceError: MySite is not defined
at HTMLImageElement.onclick ((index):1)
Is there any advice anyone might be able to give to help me get around this. I have done some googlinig but not getting any direction.
Any advice is greatly appreciated!
You'd better don't name a class as the same with a namespace.Try to change the Entry name,for example,EntryModel.And then use
#model IEnumerable<EntryModel>
partial view:
#model EntryModel
Uncaught ReferenceError: MySite is not defined at
HTMLImageElement.onclick ((index):1)
The error is usually happended when a function in onclick event is not defined.For example:
<button onclick="MySite()">click</button>
and if MySite() is not defined in js,it will get the error.

Remove red * in required fields Yiibooster

I'm using yiibooster and it is really good extension for the frontend, my issues now is that I want to remove the red * that is rendered in the required fields but maintaining the required validator in the model, anyone knows how to do this????
thankss
This is an example of a label generated by a required field validator:
<label for="User_email" class="required">
Email Address <span class="required">*</span>
</label>
Therefore, you can hide it by adding this class to your site's CSS:
span.required {
display: none;
}
If you want to achieve what you want easily, I suggest you to do like below, which is simplest way(in my view point):
Just try to find * selector(the ID or CLASS) name.(using a firebug or any inspector)
Then just do like below in your document.ready():
$(SELECTOR).remove();
NOTES
THE * MIGHT BE CREATED DYNAMICALLY
THIS IS JUST AN SUGGESTION, YOU CAN FIND ANY OTHER POSSIBLE WAYS SUCH AS CHANGING THE CSS CLASS IN ORDER TO DO DISPLAY:NONE OR SOURCE MODIFICATION
<?php echo $form->textFieldGroup($model, 'username',array('label'=>Yii::t('model','Username'))); ?>
or edit line 1223 of TbActiveForm.php from
echo $this->labelEx($model, $attribute, $options['labelOptions']);
to
echo $this->label($model, $attribute, $options['labelOptions']);
Red * is adding according to your validators definition in your model. you have two options.
First in your model add On => 'scenario name' for required validator for the property you want. so you can control the behavior of yii-booster components because they only apply those rules which matches the scenario of the model. for example:
array('password_repeat', 'required', 'on'=>'register'),
It will show Red * only in register scenario (if you set it via $model->setScenario('register');) and in normal times no red * will shown.
Another option for you is when you are creating the form element based on the property marked required by validator rules in model, you can prevent that * from showing but this way will not ignore that validation rule and if you try to submit the form while this form field is empty you will get error from yii (because you just solve showing but in background you have your required validator). for this method, you only need to provide label in your yii-booster form element:
<?php echo $form->textFieldGroup($model,'textField',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'hint' => 'In addition to freeform text, any HTML5 text-based input appears like so.',
>>>>> 'label' => 'Your new value for label which will have no red *',
)
); ?>

rspec rendered view fails to include variable data

I have a failing rspec view test but the code works - I probably have a variable incorrectly setup but can't figure out what it is.
When I display the contents of #incident_report (pp #incident_report) in my spec, it properly displays the record created by FactoryGirl.
When I display the actual rendered content (puts rendered), it shows the values from the the record I created with FactoryGirl...
But the "rendered.should contain(work_order)" spec fails with:
1) incident_reports/show.html displays the work order number on the incident
Failure/Error: rendered.should contain(work_order)
expected the following element's content to include "54785":
and none of the data is displayed, only the HTML template
spec/views/incident_report/show.html.haml_spec.rb code
require 'spec_helper'
describe "incident_reports/show.html" do
before(:each) do
#incident_report = Factory(:incident_report)
end
it "displays the work order number on the incident" do
work_order = #incident_report.work_order
pp #incident_report #displays an incident_report, id => 1
assign(:incident_report, #incident_report)
render
puts rendered #this DOES have the content from #incident_report
rendered.should contain("Work Order:")
rendered.should contain(work_order)
end
end
show.html.haml code
%h1 Display Incident Report
.navigation
= link_to 'Edit', edit_incident_report_path(#incident_report)
|
\#{link_to 'Back', incident_reports_path}
= render 'form'
.navigation
= link_to 'Edit', edit_incident_report_path(#incident_report)
|
\#{link_to 'Back', incident_reports_path}
Gotta be something really simple I'm overlooking.
Turns out it's because I was using simple_form and when simple_form displays for a "show" action, it puts the field values into the html as a 'value="54785"' attribute. If you display it in a browser, the labels and values all show up correctly, but rspec can't see them.
I had to add
rendered.should have_tag 'input', :with => { :value => "54765", :name => 'incident_report[work_order]' }
to my example to get it to work.
Seems like there should be a better solution but at least now I can continue testing.

submitting a form without submit button

I am new to structs... I has an application in that there is drop down list with in a form like this..
<%#taglib uri="/WEB-INF/struts-html.tld" prefix="prefix1" %>
<prefix1:form action="formaction">
<prefix1:select property="choose">
<prefix1:option value="1">1</prefix1:option>
<prefix1:option value="2">2</prefix1:option>
<prefix1:option value="3">3</prefix1:option>
<prefix1:option value="4">4</prefix1:option>
</prefix1:select>
Now my problem is, i want to submit the form upon onchange() event instead of having a submit button. i.e, when ever a value from drop down list selected the form has to submit..
How can i do it??
Edit: As mentioned in Quentin comment, you shouldn't submit your form onchange of drop down selection. Read here
But if you still want to do it then, A simple javascript will do the trick, Provided javascript is allowed to use in your application.
Add a name to your form tag, <prefix1:form action="formaction" styleId="formAction">
Add below function inside <script> tag in your jsp
Add an onchange event handler to the select element, <prefix1:select property="choose" onchange="submitForm()">
function submitForm () {
document.getElementById("formAction").submit();
}
I got my solution..Its looks like this
<prefix1:form action="sample">
<prefix1:select property="choose" onchange="this.form.submit()">
<prefix1:option value="1">1</prefix1:option>
<prefix1:option value="2">2</prefix1:option>
<prefix1:option value="3">3</prefix1:option>
<prefix1:option value="4">4</prefix1:option>
</prefix1:select>
</prefix1:form>

Reload calendar into nested forms (rails 3)

I have a form which contains one model (let's call it Model1) and this models accepts nested attributes from another one (let's call it Model2). I want to be able to save many Model2 records, but I want the view, at the beginning, to show just one set of fields from Model2, and have a button which, if clicked, displays another set of fields from that model, and so on. For this, i'm using the nested_form gem.
All is working well, but the problem is that one of the fields is associated with a datepicker. So, the first Model2 set of fields shows the calendar, but when I click to render the next set of fields, the datepicker, of course, does not get shown anymore (because the Javascript loads only when you get to the page and never again)
The following code shows the button which adds more concepts (Concept is Model2)
<%= f.fields_for :concepts do |concept_form| %>
<%= render "courses/concept_fields", :f=>concept_form%>
<% end %>
<p><%= f.link_to_add t("concepts.add"), :concepts %></p>
And inside courses/concept_fields I have:
<%= f.text_field :collection_date, :value => f.object.collection_date, :class => 'text date_picker tabs' %>
The javascript for the datepicker is included in the layout
Is there any way you can help me? Thanks!
Add and click event handler to the button you use to render the next set of fields to reload the datepicker something like:
$('.render_next_button').click(function(){
// code to reload datepicker
$( "#datepicker" ).datepicker();
});
I've found an answer which solved my problem. It's in this link:
jQuery UI DatePicker with nested_forms Gem
Would look like this:
<script type="text/javascript">
jQuery(function() {
jQuery('form').live('nested:fieldAdded', function(event) {
jQuery(event.field).find('.date_picker').removeClass('hasDatepicker').datepicker();
});
});
</script>