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].
Related
Sorry for rookie question but I want to pass params to controller not related with that view.
I got view with Teams and controller where user adds own favourites teams by marking in Team index.
For example pass additional params to link helper like
link_to 'fav', addfavoriteteam_user_path(team_id:team.id)
and they will be passed to addfavoriteteam action of users controller (provided you have routes set for it)
More RESTful way is to have favorite action for teams.
Or if you want to have a form with checkboxes, you can have it around teams#index, just set form action url to point to another controller
You probably also want accepts_nested_attributes_for for your many-to-many relation model in this case
I have a form which has two buttons
One will assign the form, i mean create an entry in corresponding table after validation and redirect to another page
Second will give a preview of the values submitted on the form.For the preview screen we need some calculations so that an action to be called from controller and respective page will be loaded.
This page should seen on the same form if i clicked preview before assigning the form
How i would address this is to change your new action so that it takes the same params as the create action, and makes the object but doesn't save them. This means that if you call the new action with the params it will reload the form with the built but not saved objects' data displayed in it.
The two buttons will need to change the form's action attribute so that it submits to the create or the new action as appropriate.
This is a general answer, as your question is quite general!
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.
Wasnt sure how to word the question, but this is the scenario:
the view is a data entry form eg http://127.0.0.1/User/AddEdit/
so edit the user I have an ID: http://127.0.0.1/User/AddEdit/7838fd9c-425c-4c98-b798-771bba10d9c1
This ID gets the data to populate the form values in a ViewModel, which populates the form
I am using jquery/ajax to save the form, which returns a Json result, indicating ok/error etc
In the View, I get the ID and use this in a hidden field which is set via jquery when the page loads and when the form is saved via ajax.
This seems a bit clunky, how do others do this?
in my opinion best solution is to create a partial view with all the fields and use it on add and edit view which are separate actions in controller. after you create user you can redirect to action edit. if you must / like use ajax you can reload div with form (change from user/add to user/edit/1). i might be wrong but i never see a code or example with one action in controller for add and edit.
I'm currently re-using a partial on two different views.
View A
View B
The partial belongs to Model A but has an association with Model B so it is applicable to Model B as well.
It contains a form and when data is submitted, it always redirects the user to View A. However, when I submit the form from View B, I would like to be redirected back to View B instead of Form A.
The reason it redirects right now to View A is because that's the model this form belongs to. So when posted, it talks to controller A and uses a redirect take the user to a_url.
How can I tell my form (or more so that controller action) to redirect the user back to where they came from?
Thanks!
Solved.
I added a hidden field to my form that contained the controller name of where the partial was rendered and then my respond_to block determined where to send the user.
View code:
<%= hidden_field_tag 'submitted_from', "#{controller.controller_name}" %>
My controller code:
if params[:submitted_from] == 'A'
redirect_to a_url
else
redirect_to b_url
end
Instead of using a hidden tag, you may want to place this in the session:
session[:submitted_from] = new_model_url
and in the other action..
redirect_to(session[:submitted_from])
session[:submitted_from] = nil
It's pretty easy to switch out form variables, whereas it may be harder (but not impossible) to forge a session. I'd go this route if it were my application.