I have users that can choose which 'areas' they cover.
The relationship between 'users' and 'areas' is a has many and belongs to.
I can currently get collection_check_boxes to list the 'area' options for the user but I'm stuck on getting the areas to be updated once the user chooses and submits the area options.
Here's my form:
<%= form_for #user do |f| %>
<%= collection_check_boxes(:user, :area_ilondon_ids, AreaIlondon.all, :id, :postcode) %>
<%= f.submit %>
<%end%>
The form above renders a list of checkboxes showing the users current 'area' selection but when I submit the form the areas do not get updated.
What do your user_params look like? Do you have something like this :area_ilondon_ids[]? You need to let in the array of ids.
As #Stoob pointed out the following was needed:
:area_ilondon_ids=>[]
Related
I am very new to Ruby and Rails, and I have come across a problem I can't seem to be able to solve:
In my rails app i have users which on the index site, called home, are displayed a couple of events.
If the user wants to "bookmark" these events he can click on a button next to the event an add it to his event_items list, which is just an "association"-controller I implented for the association between users and events (e.g. users has_many events and vice versa)
This all works fine, and this is how I have implemented the add functionality:
index.html.erb extract for home:
<h2>Events</h2>
<% #events.each do |event| %>
<div><%= event.eventtitle %></div>
<%= button_to 'Add Event', event_items_path(event_id: event, user_id: session[:user_id]) %>
<% end %>
The button_to method works fine to add an event to the event_item list which just records the user_id and the event_id for an association in an even_items db-table.
Now I would like to allow the user to add a comment along to the event to the event_items db-table.
I can't seem to find a way of passing a text_field value to my event_items_path to add the comment to the event_items table in the db.
I guess I can't use the "button_to" method anymore because it is resolved in it's own form and thus I can't transmit any text_field information. So I created a new form... I can't get this form to pass the text_field information as parameters though:
new index.html.erb extract for home:
<h2>Events</h2>
<% #events.each do |event| %>
<%= form_for :event_items, url: event_items_path(event_id: event, user_id: session[:user_id]) do |f| %>
<div><%= event.eventtitle %></div>
<%= f.label :event_comment %><br />
<%= f.text_field :event_comment %>
<%= f.submit %>
<% end %>
<% end %>
When I press the submit button, it still creates the event_item in my db-table, but withouth the event_comment. Checking the post transmission in my browser it doesn't seem to transmit the :event_comment.
It can only access the :event_id and :user_id in the parameters.
I would appreciate any help!
Update
I have just now realised, that when pressing the submit button from my new form above the :event_comment does get transmitted with the form.
Checking in Chromes DevTools, in Network>>Headers there is a post request which has event_id and user_id listed under Query String Parameters and the event_comment listed under Form Data as event_items[homeprediction]
Does anyone know how I can access the Form Data? Using params[:homepredictions] doesn't work in my event_items controller.
Found a solution now:
The :event_comment was being transmitted on submit. It is stored in Form Data as event_items[homeprediction].
This is what I did in the event_items controller to access the data:
def create
#user = User.find(params[:user_id])
event = Event.find(params[:match_id])
comment = params[:event_items]
#event_item = #user.event_items.build(event: event, event_comment: comment[:event_comment])
...
end
I saved the params[:event_items] hash to a new comment[] hash. And in the new comment[] hash I was able to access the :event_comment passed from the form field_tag.
Don't know if this is the most elegant/right way of doing it. But it works for now :-)
I have 3 models. Users have multiple Portfolios and Portfolios have multiple Assets.
When a user signs in, that makes him a current_user by find_by_id. When they first register, it creates a portfolio for them. That becomes the current_portfolio.
Subsequent to this, they can create portfolios which redirects them to the list of all their portfolios. Clicking the link to each will make that portfolio the current_portfolio.
#current_portfolio = current_user.portfolios.find_by_id(params[:id])
The show view for portfolios has an assets form so that they can quickly add assets to the portfolio. This is where I get stuck. Because the assets form is on the portfolios show view, my Asset controller needs to reference current_portfolio, but this is in my Portfolio controller.
Portfolio Controller:
def show
#current_portfolio = current_user.portfolios.find_by_id(params[:id])
#asset = #current_portfolio.assets.build
end
When the form is submitted, it goes to my Assets controller Create function. Create can't simply have #current_portfolio because it's on a different controller. I also can't use find_by_id(params[:id]) because :id is not providing anything.
How do I reference the second model when I have 3 models? (Sorry I'm a newb to rails...)
EDIT Update: I thought I found the solution but realized it didn't work.
I passed <%= hidden_field_tag :portfolio_id, params[:id]%> in my form and set my Create in the Asset controller to be
def create
#asset = Asset.new(params[:asset])
#asset.save
end
but it's not getting the portfolio_id which is a required field in my model.
Full form in my view
<%= form_for(#asset) do |f| %>
<%= hidden_field_tag :portfolio_id, params[:id]%>
<%= f.text_field :asset_symbol %>
<%= f.text_field :shares %>
<%= f.text_field :cost %>
<%= f.text_field :purchase_date, :type=>'date' %>
<%= f.submit "+ Add to Portfolio" %>
<%end%>
My guess would be that the portfolio_id in the hidden field isn't coming back as part of the asset hash. Maybe it's as simple as assigning the #asset.portfolio_id = params[:portfolio_id] before the save. Check you logs to see EXACTLY what is coming back in the POST
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.
I was stuck with a different scenario like say for example i have model movies and the other is releases. I want to create a new form in the apps/views/movies/new.html.erb. In the new i wanna have the fields for movies table eventually ill be having a form for it and at the same time i want to add the movie release information to the other table releases.Like it will be nested form as follows
<%= form_for(#movies)......%>
<% files for movies table %>
2nd form <%=form_for :releases %>
<% fields for releases table%>
so form one carries the data to movies table when user clicks on submit button. But i want to pass the the values to release table also with this click using a nested form. Is it possible to have nested forms. If so i wanted some help on how it can be achieved exactly. Help me out please.
<%= form_for(#movies) do |f| %>
# fields for movie
<%= f.fields_for :releases do |nested_f| %>
#fields for your nested form releases
<% end %>
<% f.submit %>
<% end %>
Also in Movie.rb model add this line. accepts_nested_attributes_for :releases
More information there
I don’t think HTML allows nested forms, see for example this question.
I guess this is a newbie question, but what is the syntax in a form to show contents of fields not as a text box/area, but rather like label would appear.
<% form_for #user do |f| %>
<% f.fields_for :user_ingreds do |builder| %>
<p>
<%= builder.??? %>
</p>
<% end %>
<% end%>
user has_many :user_ingreds and accepts_nested_attributes_for :user_ingreds.
Basically I want to make a list of user_ingreds where the user can't edit the data but can remove the record from the list via a button.
However the fields_for builder doesn't recognize a direct call to to the fields in UserIngred model (ie, builder.user_id throws and error.
If you want to make a non-editable list of the ingreds with a button to remove them, then don't make a form.
Instead, display your data and a button beside them created with link_to and pointing to your delete function.
If ever you want to really display the content of your form variable, you can access it this way:
builder.object.user_i