ThymeLeaf th:with object property -> how does it fetch properties? - thymeleaf

I have an Owner object that inherits a BaseEntity that has method isNew() that checks if this.id==null;
In thymeleaf template, I have th:with="text=${owner['new']} ? 'Add Owner' : 'Update Owner'"
Can someone please explain how thymeleaf links the isNew() method with the ['new'] property of Owner?
I'm just confused b/c there is no explicit 'new' property but is just a method.

Related

Rendering view, but no object in gsp

I try to render a view, which works fine, but it doesn't seem to get the model object I pass along to it. I can't figure out the reason, as this should be very straightforward according to all manuals and examples.
Model object
class Race {
def distance = "1/4 mile"
def racer1
def racer2
}
RaceController renders here
def doFullRace(Race race) {
render (view: 'raceProgress', model: [race: race])
}
and raceProgress.gsp should display it easily enough
<html>
<body>
<div id="raceStart" align="center">
...
<p>${race.racer1} is racing ${race.distance} against ${race.racer2}</p>
</div>
</body>
</html>
but I instead I get this
Any ideas on what basic thing I missed?
You have the following:
def doFullRace(Race race) {
render (view: 'raceProgress', model: [race: race])
}
One of the ways for race to be null there is if all of the following are true:
Race is a domain class
The request submitted to doFullRace includes a request parameter named id
There is no record in the database with an id that matches params.id
From http://docs.grails.org/3.3.9/guide/theWebLayer.html#commandObjects...
If the command object’s type is that of a domain class and there is an
id request parameter then instead of invoking the domain class
constructor to create a new instance a call will be made to the static
get method on the domain class and the value of the id parameter will
be passed as an argument.
And...
If the command object’s type is a domain class and there is no id
request parameter or there is an id request parameter and its value is
empty then null will be passed into the controller action unless the
HTTP request method is "POST", in which case a new instance of the
domain class will be created by invoking the domain class constructor.
For all of the cases where the domain class instance is non-null, data
binding is only performed if the HTTP request method is "POST", "PUT"
or "PATCH".

Activeadmin set resource's attributes on action

I want to set a default value for an attribute of the activeadmin resource, so when it renders the form, it will use this value.
e.g.: in the action new, I want to set the attribute product of the object order , so the form will come with the product already selected(but leting it to be changed).
Just add the value to the form field as follows:
f.inputs do
f.input :product, input_html: { value: products_value }
I accepted Andrey Deineko's answer, but even before I saw it, I've done it in another way:
controller do
def new
#resource_name = ResourceName.new(...)
so, I just defined the action method inside the block controller, creating an instance of the resource with an instance variable named as the ActiveAdmin Resource name.

When do we need the instance variable and when do we need string name for a partial

I am reading Agile Rails Web Dev book and so far for partials I had learned than we can call their name in a string form or if there is a collection of objects we can pass the object name and rails will figure out that it needs to loop through it as a collection.
Now I saw this code and suddenly all I had learned got confused:
<%= render #cart %>
My question is what is it #cart, Why it is not 'cart' ? And how should I have known that?
The Rails Guide suggests that when you do "render #cart", Rails introspects the model name of #cart and looks for a partial by that name in the current view path.
The implementation of render :partial actually calls to_partial_path on the passed object. A User object would by default return 'users/user'. So I'd check to see if the Cart class implements to_partial_path to return 'layouts/cart'.
I could be wrong but I believe that behind the scenes Rails will render a partial for anything that has a to_partial_path method. In this case I'm assuming #cart is an ActiveRecord object and all ActiveRecord objects respond to to_partial_path.
Try adding this to your template and see what it outputs. It should be the path of your partial.
<%= #cart.to_partial_path %>

Submitting custom information to a controller

This is probably simple, but I've tried a few things and couldn't find a way to make it work.
I would like to update a model with custom information given in a form_for
To make it more concrete, I'm on the show page for a particular instance of MyClass and I would like to pass something like the string "yay" into the controller, and then do as I please with the input. Maybe pass it back to the page as a flash message, or maybe modify the contents and then store it as a field of the MyClass instance.
I can write form_for's that contain the attributes of MyClass without prbolems, but it seems that other fields throw an error.
How do I write the form_for so that I can accomplish one of the two above scenarios?
def update
#my_class = MyClass.find(params[:id])
flash[:notice] = "This works" # but what can I write in a form for for it to be a variable that's passed in?
#rest of the update
end
Form helpers that unitize a form builder instance (like f.text_field) expect a valid model attribute so it can generate the appropriate id and populate the field with data from the model. If you want to have form fields that do not correspond to model attributes, don't use the the standard f.text_field but instead use:
<%= text_field_tag 'my_custom_tag' %>
which should render something like:
<input type="text" id="my_custom_tag"></input>
When the form is submitted, the value of the input will show up in the params hash with a key of :my_custom_tag.
I hope this helps.
It seems that you would probably need a hidden_field in your form :
http://apidock.com/rails/ActionView/Helpers/FormHelper/hidden_field
However, if you wish to save some kind of state, which seems like this is what you want, you would never use that. Instead, you would use a session. The reason is that a hidden field can be manipulated by the client and thus security can easily be overridden.
Like Spyros said, a hidden field will give you the place. Assuming you are ok with the fact that a user can modify the URL, just add attr_accessor :foo to your model.
In the controller you can access it with bar = params[:foo] and do as you please.

Dynamically generating method names in rails

I need to generate links in my views using the url helpers such as user_path(#user), the catch is, in some cases I don't know what model I am creating this link for i.e. whether it is a user or a store or someting else
I would like to be able to determine this on the fly and call the appropriate view helper, currently I am doing the following, but I am wondering if there is a drier way of doing it.
if object.class == "Store"
store_path(object)
elsif object.class == "User"
user_path(object)
...etc
Sure, use send to dynamically choose the method name
send("#{object.class.name.downcase}_path", object)
url_for(object) does what you need:
If you instead of a hash pass a record
(like an Active Record or Active
Resource) as the options parameter,
you‘ll trigger the named route for
that record. The lookup will happen on
the name of the class. So passing a
Workshop object will attempt to use
the workshop_path route.
If you are using link_to, then you can just pass the object as the URL:
<%= link_to 'Title', object %>

Resources