I am wondering how can i create a field not associated to the models, the only reason i need the field is to determine which actions to do on it.
Lets say i have a model article, and when creating a new article, i would like a hidden field that would have 0,1,2 and in the controller new, i would see if the params is equal to 0, then do this set of logic or 1 then this set of logic.
Thank you, I also know that defining a set of action for each action won't work.
In a form you can declare both hidden and visible fields that are not directly associated with your models. When you submit the form, in the form's action you can manipulate the attributes in the params that are not related to the model.
When you declare form fields you can use those that end with _tag like email_field_tag, radio_button_tag, and regarding your question, hidden_field_tag. Example:
<% hidden_field_tag 'this_is_hidden' %>
Try it out and inspect what comes into the action: raise params.inspect. In doing so you'll notice the params now includes keys for the attributes you declared that are not related to your model (like the attribute :this_is_hidden)
Try doing it with a hidden_field_tag. (recommendation: put it just before the submit button inside the form tag.)
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-hidden_field_tag
hidden_field_tag 'decide', '0'
Then in the new action of the controller you can catch it inside the params hash, and compare it with params[:decide].to_i
most easiest way is to have a hidden field as #Zippie, mentioned. However it has some risks as end user could modify the value in the hidden field and your program could behave differently.
What i personally believe is to have a some kind of a methodology to identify from the passing parameters
Ex: if it is a new object then it should go to method A etc...
By that way end use will not have a chance to modify the workflow.
Related
I have a comments model, and within that model, I have a field called :honey.
In the view, :honey is a hidden form field.
In the controller, when the form posts I want it to redirect to the home page if :honey is filled out.
How do I call that specific field in my controller?
If you're talking about when your form posts back to your action, you should be checking params. Either look in your logs or use binding.pry and look for the key and value you're expecting.
It will probably be params[:comment][:honey].
I've got a form with some check_box_tags. Some of these checkboxes do not belong to the model and need to be removed from the params hash. I know i can use the exclude method, but the problem is that these fields have dynamic ids that I can't know in advance.
My question is:
Is there a way of excluding a form field so it doesn't enter the params hash in the first place?
Many thanks!
Uri
Anytime you submit a form to a controller and the input fields have a name, those K:V pairs will be added to the params request assuming they are white listed by your strong parameters (rails4 only). Your best option would be to use JavaScript. Something like this would filter those fields based on class, data attr, or any other parameter you would like to filter by:
$('.new_resource_form').onSubmit(function(e) {
e.preventDefault();
$('.fields_for_removal').remove();
$(this).submit();
});
I have created a instance variable in rails project, which gets its value from a url parameter like example.com/value. This variable is created in new action, now can it also be used in create action, of the same model.
The value is a id of another model altogether and both the models are associated, I need to create the instance variable in former model.
I need to know, for how long the instance variable is available, and can be use the instance variable of one model in another model.
Clarification with real example
Supposingly there are two models, one is User model and other is Referral model. The root is root :to => 'users#new. Now the user will coming here via example.com/value, where value is the id for Referral model. Now using this value I have to increment two fields: One is visits, which shows how many visits did that particular url bring. Other is signup, which will increment if there is a signup using that value.
I have passed this value via routes in users#new, which I use to increment the visits column of Referral model. Now if the users signup, the users#create would be executed, and I want to be able to use the value in the create action as well, to increment the signup column in Referral model.
As of now, I understand that the instance variable I created in new action to store the value cannot be used in create action. Now how can I achieve this.
In general instance variables only last as long as the user's HTTP request, so they can not be created in one action and used in another.
You could try storing the variable in the session, a hidden input field on the HTML form generated by the new action, or in the urls of links generated by the new action.
I don't know exactly what you are doing, but from the names of your two actions it sounds like there is probably an HTML form involved, so I think the best thing is to use a hidden input, something like this:
<input type="hidden" name="model_id" value="<%= #model_id %>" />
Instance variables only last for that call and in the class they are defined, with the exception of the views. If you have a controller with two methods where one method is your route and another is used internally, then it will be available to both, it is also available to your views.
e.g.
test_controller.rb
def index
something_else
p #variable #outputs "foo" in the terminal
end
def something_else
#variable = "foo"
end
However it would not be available between create and new as these would be called in different requests.
I have an edit view that is displaying some model properties as plain text and other model properties as input fields. My problem is that the model properties displayed as plain text are null when the model is returned to the view after a validation error.
How do I retain the model values when I don't have input fields for all properties? What is best practice?
My first thought is to keep the values of these properties in hidden input elements.
<%= Html.HiddenFor(model => model.CustomerName) %>
Is this considered a correct solution?
Thanks
Andreas
You can either:
Use hidden fields, as you suggest, or
Re-populate the "missing" fields before returning the view in the POST action.
Pick whichever works for your app.
I'm attempting to create a hierarchal menu system in rails.
My menu model is in a tree data structure, and I need to pass the parent information to the new action. The best way i found to do this was to pass the parent's id as a querystring. This works, unless validation blocks creation. The problem is that after the new view is rendered, the parameter is no longer present in the url.
Am i going the wrong way with this? Should I just be persisting the value in the session? Should i create a dropdown to pick the parent item (I'd prefer to just let the user click "Add new item here"). This must be a common issue, but I havent been able to find antything relevant.
Update: To clarify a question, I tried doing the following to pass the parameter when the view is rendered in the failure case, but the url has already been set at that point (we've already posted to the controller's base url).
format.html { render :action => "new", :parent_id=> params[:parent_id] }
I need another way to pass the parameter.
I figured it out. I just added a hidden field to hold the parent_id. When the browser requests the new form, i put the parent_id into the model object, and it's persisted on psotback.
Not 100% clear on what you are trying to achieve.
After the operation fails validation, you render the view again?
Can you not just pass the parent_id to this view as a parameter?