I want my app to let the user choose between a few addresses or create one. The address is to be stored inside the contract object (Contract.adresse_id).
The controller generating the page displaying the form generate a #user variable.
The goal is to either pass the id of an existing address to the next controller, or to pass the params of a new adress (classic nested). This page only goal is to set the adress, no other fields of Contract should be modified.
The model Contract has a address_id field. I want a radio button allowing to choose between :
#user.address # it is an address id
#user.secondary_adress # address id too
and an empty form allowing to create your own address.(classic nested)
i guess it should look like :
<%= form_for contract do |f|%>
<% f.label :address%>
<% end%>
But then i do not know what to do. How can i do a form allowing to chose between 3 addresses where 2 already exists and the third is to be created ?
You could make your third radio option an 'id' of 0 or -1. Then in the next controller when you look up the Address, you instead make a new Address if the id is your fake non-id. You could probably use the same view in either case on that next controller by setting the url of that form based on whether the Address is new. eg:
- if #address.new_record?
- form_url = [create path]
- else
- form_url = [update path]
...
= form_for(#address, :url => form_url) do |f|
(That's in haml instead of erb, obviously. I highly recommend making the switch)
My problem is in 2 part :
1) choose an existing address to put it in contrat
2) add the possibility to create a new address instead of the existing ones in the same page
For 1) i did this :
<%= form_for #contract ,:action => "confirm" do |f| %>
<div><%= f.radio_button 'address',#user.address.id, :checked => true %>
<%= render #user.address %>
</div>
<div><%= f.radio_button 'address',#user.address_secondary.id, :checked => true %>
<%= render #user.address %>
</div>
<%= f.
<%= f.submit %>
<% end %>
It simply submit the id of the chosen existing address
2) Instead of making a complicated single form allowing to chose between the two, i did a second form with a nested new address. It imply i create a new address in the previous controller, but if i do not use it, it's not a problem.
In the end my page have two different form submitting different data but it is way easier to do.
Related
Noob question! :)
I have a form, that has basically no point other than call some_action. The reason I use a form for this, is because we have a specific styling for this in our large website.
<%= styled_form_for(#user, :url => some_action_user_path #user)) do |f| %>
<%= f.save_button %>
<% end %>
I thought, since it's a form, I should be able to put a checkbox in there. It should have no other goal than confirming the user wants to do this action indeed. E.g. "Yes, I want to do some_action to the user model".
How would I make a checkbox that does not really change any attribute or affect anything - Other than that it should be checked for the form to submit?
This is probably dead simple, but according to the documentation and various error messages I should provide arguments such an attribute (which I don't want...)
form_for is meant to work on attributes of a model, which is what all the documentation you are reading is telling you. So if your model had a boolean column you could easily attach a check box to it.
If you ever want a form (or specific tag) that does not follow this, you can use the _tag version of these methods. For example, form_tag or, in your particular case, check_box_tag.
Example:
<%= styled_form_for(#user, :url => some_action_user_path #user)) do |f| %>
<%= check_box_tag "do_some_method" %>
<%= f.save_button %>
<% end %>
NOTE: You will only get a param entry for :do_some_method if it is checked off. If you want to get a param regardless, you have to add a hidden_field_tag before it.
<%= hidden_field_tag "do_some_method", "no_dont_do_it" %>
<%= check_box_tag "do_some_method", "yes_do_it" %>
Now if the checkbox is selected you'll get params[:do_some_method] set to "yes_do_it"; if it's not checked off, instead of getting no entry, you'll get params[:do_some_method] set to "no_dont_do_it".
I am working on a rails web app which manages students and courses. I have a courses controller which has the following index action:
def index
#courses = Course.paginate(:page => params[:page], :per_page => 1)
#courses.order(:startDate)
##thisCourse = Course.find(params[:page])
end
So pretty standard except for one thing - all the details of a single course are shown on one page and to show the details of the next course, you move to the next page of the pagination.
The problem is that in this index page showing the details of 1 course per pagination, I have a "Sign Up!" button which when pressed needs to create a a new record in the 'signups' db table which has the automated 'id' field and then the 'user_id' and the 'course_id' fields.
The 'user_id' is easy to find (current_user.id).
The 'course_id' is proving difficult. I imagine that pressing the Signup button should send the course_id to the signups_controller where a create function can do the work. But how can I get this exact course ID from the index page to the signups_controller's create action?
As you can see in the code I pasted from the courses_controller's index action,the '#thisCourse' variable has been commented out because I have found no way to define which course is currently being shown on the page.
The fields are rendered by the will_paginate Gem so I'm not sure how it's generating the fields but I was thinking that maybe I could create a named hidden field which includes the course_id and use that when the sign up button is pressed, however I'm not sure how to go about it.
Does anybody have any ideas?
Thanks!
Well, you can use show method (output one course) instead of index(output all courses) method, that will always get your course id through params.
Basically I changed my approach to the problem. I removed the button which was supposed to call the signups_controller and create the new record in the signups table. This button was replaced by adjusting the form_for helper so that it's submit button would send all the necessary data to the signups_controller (including the id value which was added to the form as a hidden field).
The form ended up looking like this:
<%= form_for course, :url => {:controller => "signups", :action => "create"}, :method => "post" do |f| %>
<%= hidden_field_tag :course_id, course.id %>
<%= f.label :"Course Title" %>
<%= f.text_field :courseTitle, class: 'form-control' %>
+ all fields included in the form....
<%= f.submit "Sign Up!", class: "btn btn-primary" %>
<% end %>
This parameter of form_for defines which controller and which action in that controller is the submission target:
:url => {:controller => "signups", :action => "create"}
and this parameter overwrites the default http action (default is PATCH but in this case I wanted to POST i.e. create a new entry in the signups table):
:method => "post"
I'm not sure if this is a very quick and dirty solution but technically it gets the necessary data to the correct destination controller.
In my rails app, if a user mentions another username in a comment by using the # character, such as #max i'm trying to add autocomplete to suggest a list of users and then automatically create a link_to (username, user_path(user)
This is what I have in my comment partial:
<%= form_for [commentable, Comment.new] do |f| %>
<%= hidden_field_tag :commentable_type, commentable.class.to_s %>
<%= hidden_field_tag :commentable_id, commentable.id %>
<p>
<%= f.text_area :body %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
I'm trying to use this gem: https://github.com/ichord/jquery-atwho-rails
It says to bind the text area with
data = ['tom','john'];
$('textarea').atwho({at:"#", 'data':data});
Where do I actually put this? Can I do something like data = User.all? Should I just be using a regular expression to do this?
I think the reason that data = User.all isn't working is because User.all will return an array of User objects. What you want to do is retrieve those User object usernames (or whatever you want the autocomplete to use, and store that in data instead.
You might try something like
#usernames = User.pluck(:username)
to get all the usernames. Then, in your partial:
data = <% #usernames &>
$('textarea').atwho({at:"#", 'data':data});
This is assuming of course that your partial is an .erb file where you can embed ruby code.
You can do something like this:
<script>
data = <%= raw User.pluck(:username).compact.to_json %>;
$('textarea').atwho({at:"#", 'data':data});
</script>
You might want to move the loading of the usernames into the controller or a helper method. The whole sniplet might belong into an view partial to keep things organized. And it might not be the best idea to load all usernames into the view when there are too many users in the database.
below is my code
<% if #user.empty? == true %>
<p> Sorry no data to display</p>
<%else%>
<% #user.each do |n|%>
User id = <%=n.id%> <br \>
User type = <%=n.type%> <br \>
User name = <%= n.name%> <br \>
<%= link_to "Good. You can proceed on creating a new", new_user_product_path(current_user)%>
<%end%>
<%end%>
This code is currently under searches#index. Now as you can see, after a set of results is shown, I want the user to be able to create a new product. But when creating the product, I want to make sure <%= n.name%> is pass over to new_user_product_path(current_user) (its a form). But not via url.
The form field which i want to populate is
<%= f.hidden_field :user_name%>
So, how do I do it?
Thanks
I think i found a solution
I just made <%=n.name%> to <% $name = n.name %> and since a global variable, its accessible now.
You don't need to pass name from search index, as you have current user reference:
<%= f.hidden_field :user_name, current_user.name %>
This will render a hidden input with a value of current user name.
Even you don't need this parameter to be passed after user submits form.
If you are doing this I suppose is to have the user name reference available in client, to do something like this in Javascript:
alert($('[ name = user_name ]').val());
Have a page where there are multiple input fields of the same thing, Posts. Right now, when a user enters in a question for, let's say 3 fields, the only one that saves to the database is the last one. Whereas, it should save all three and give them each it's own post_id. Also; if the user doesn't enter anything in for the other fields, it should not save in the database either.
<%= form_for(#post) do |f| %>
<%= f.text_field :content %>
<%= f.text_field :content %>
<%= f.text_field :content %>
<% end %>
It's failing because what you've got above evaluates to thee html field with the same name/id and the browser will only post the value for one of them. If they are different fields, then you need to give them unique names/ids or you need to create them as an array eg:
<%= f.text_field_tag 'content_array[]' %>
or, if you want these to be a set of posts - you'll need to add multiple sub-forms (one for each post) using a custom form.
What you can do is convert to html and as an array
in your form:
<input`type="text" name="post[content][]" id="content_id">
Then, in your controller:
content_details = params[:post][:content]
content_details.each do|cont|
#post = Post.new(content: cont)
#post.save
This will loop through all of the content created and save each.