create function in controller not saving two separate params - ruby-on-rails

these are the codes
<p>
<% form_for #movie do |m| %>
<%= m.label :title, 'Title' %>:
<%= m.text_field :title %></br>
<%= m.label :release_year, 'Release Year' %>:
<%= m.select :release_year, (1900..2011) %></br>
</br>
<% Movie.genres.each do |genre| %>
<%=h genre %>
<%= check_box_tag :genre, genre %></br>
<% end%>
</br>
<%= m.submit "Save" %>
<% end %>
</p>
and my code in the controller:
def create
#movie = Movie.new(params[:movie].merge(:genre))
if #movie.save!
render show_path
else
render new_path
end
But for whatever reason, I keep getting error messages saying "undefined method `each_pair' for :genre:Symbol" or "cannot find Movie without an ID". It's not saving properly.
Is it because of my submit form is only the movie form |m| or is it because my create function in the controller is wrong?
Thanks.

perhaps instead of
params[:movie].merge(:genre)
you wanted to do
params[:movie].merge(:genre => params[:genre])
?

Check Firebug in Firefox (or the Chrome developer tools if you're using Chrome, or your app logs if you're using neither), and see what parameters are actually being passed in your scenario? Sounds like your code is expecting to receive a parameter that is not actually being passed. The tools/log should reveal where the breakdown is happening.

Related

Active Storage Can't resolve image into URL error

I have set everything up for active storage and when I add a trip with an image, the app redirects to that trips show page and I can see the image. When I lave and go back to that show page I get the error
`Can't resolve image into URL: undefined method `persisted?' for nil:NilClass`
Here is the show page for trip
<h1> Location: <%= #trip.location %> </h1>
<h2> Date Visited: <%= #trip.date %> </h2>
<%= image_tag #trip.cover_photo, :style => "max-height:300px"%>
<h2> More Photos </h2>
<%= link_to "Add a photo", new_photo_path(#trip) %> <br>
<%= link_to "Public Profile", trips_path(#user) %> <br>
Here is the new trip form
<h1>Add a new trip </h1>
<%= form_for #trip do |f| %>
<%= f.label :location %>
<%= f.text_field :location %> <br>
<%= f.label :date %>
<%= f.text_field :date %> <br>
<%= f.label :cover_photo %>
<%= f.file_field :cover_photo %> <br>
<%= f.hidden_field :user_id, :value => session[:user_id] %>
<%= f.submit "Submit" %>
<% end %>
I'm not able to see the error since the photo uploads and displays correctly at first.
Had the same issue, except that I wasn't able to display the images even once. In your case, seems that the first time you are watching a temporal image, which is deleted after you live that view.
I realize that I was forgetting to add the attachment to permitted parameters in my controller, so the image wasn't saving. Your params should look like this:
def trip_params
params.require(:trip).permit(:location, :date, :cover_photo, :user_id)
end

Rendering partials failing in rails 4

Here's the partial view I'm trying to render if an user is logged in
<div class='row'>
Post a project
<% form_for #project do |f| %>
<%= f.text_field :title, class:'form-control' %>
<%= f.text_area :brief, class: 'form-control' %>
<%= f.submit "Post" %>
<% end %>
In the main view when I try calling the partial, there's no error, but the partial doesn't render
<div class='col-lg-6'>
<% if current_user %>
<% render 'projects/new_project' %>
<% end %>
</div>
This is my current_user method, defined in the application controller. I've also defined it as a helper method to use in a view template.
private
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
What am I doing wrong?
You want to render, you need to output the render's returned value using <%=...:
<%= render 'projects/new_project' %>
The expression(s) within the erb tags only get evaluated without the equals sign. In order to display the result of the expression, you need to include the equals sign; place your code within <%=...%>
Similarly, for you form_for call:
<%= form_for #project do |f| %>
...
<% end %>
You need to appreciate that <%= is an output tag
This means every time you want to output something in a non-native Ruby file (.erb etc), you'll have to use <%=
This is contrary to <% which does not output anything, but still performs the functions you specify
Partial
Also, you need to ensure your partial syntax is right:
#app/views/controller/you_view.html.erb
<%= render partial: "projects/new_project" %>
#app/views/projects/_new_project.html.erb
<%= form_for #project do |f| %>
<%= f.text_field :title, class:'form-control' %>
<%= f.text_area :brief, class: 'form-control' %>
<%= f.submit "Post" %>
<% end %>

add a tag by another user by using acts-as-taggable-on

By using acts-as-taggable-on, I made possible to add tags to a word model. And each word belongs to a user.
And I want to make it possible another user can add a tag but for a given amount of time after adding a tag the user can't add another tag.
To implement the function I'm stuck at a error params not found: word.
I rewrite a form
<%= form_for [word.user, word] do |f| %>
<%= f.text_field :tag_list %>
<%= f.submit 'Edit' %>
<% end %>
to
<%= form_tag add_tag_user_word_path(word.user, word) do %>
<%= text_field_tag :tag %>
<%= submit_tag 'Add' %>
<% end %>
then I got the params not found error, when I submit the form.
I could not find the way to pass params[:word] by using form_tag. How can I do it?
I found that I can write like "word[tag]" like this.
<%= form_tag add_tag_user_word_path(word.user, word) do %>
<%= text_field_tag "word[tag]" %>
<%= submit_tag 'Add' %>
<% end %>
Then it works as I hope.

How to submit multiple, duplicate forms from same page in Rails - preferably with one button

In my new views page I have:
<% 10.times do %>
<%= render 'group_member_form' %>
<% end %>
Now this form contains the fields: first_name, last_name, email_address and mobile_number. Basically I want to be able to fill in the fields of all the forms in one click which then submits each into the database as a unique row/id.
What would be the easiest way to accomplish this?
Note: The number of times do is called from a variable. Any advice welcome, thanks!
You should have only one form (you should put only fields in the group_member_form partial). In your view you should have something like:
<%= form_tag "/members" do %>
<% 10.times do %>
<%= render 'group_member_form' %>
<% end %>
<%= submit_tag "Submit" %>
<% end %>
and in _group_member_form.html.erb you should have
<%= text_field_tag "members[][first_name]" %>
<%= text_field_tag "members[][last_name]" %>
<%= text_field_tag "members[][email_address]" %>
<%= text_field_tag "members[][mobile_number]" %>
This way, when the form submits, params[:members] in the controller will be an array of member hashes. So, for example, to get the email adress from the fourth member after submitting the form, you call params[:members][3][:email_adress].
To understand why I wrote _group_member_form.html.erb like this, take a glance at this:
http://guides.rubyonrails.org/form_helpers.html#understanding-parameter-naming-conventions.
You can also use accepts_nested_attributes_for in your model, and use fields_for on your form.
Submitting multiple forms, afaik, only javascript, if the forms are remote: true, and you run through each of them and then submit.
$("form.class_of_forms").each(function() {
$(this).submit();
});
Alternatively a more up to date approach using form_with and fields_for, without removing the form into a partial, could be written like this:
<%= form_with (url: end_point_path), remote: true do |form| %>
<% (1..5).each do |i| %>
<%= fields_for 'cart_items'+[i].to_s do |fields|%>
<%= fields.text_field :first_name %>
<%= fields.text_field :last_name %>
<%= fields.email_field :email_address %>
<%= fields.number_field :phone_number %>
<% end %>
<% end %>
<%= form.submit "Submit" %>
<% end %>

Rails Nested Attributes Doesn't Insert ID Correctly

I'm attempting to edit a model's nested attributes, much as outline here, replicated here:
<%= form_for #person do |person_form| %>
<%= person_form.text_field :name %>
<% for address in #person.addresses %>
<%= person_form.fields_for address, :index => address do |address_form|%>
<%= address_form.text_field :city %>
<% end %>
<% end %>
<% end %>
In my code, I have the following:
<%= form_for(#meal) do |f| %>
<!-- some other stuff that's irrelevant... -->
<% for subitem in #meal.meal_line_items %>
<!-- # Edit 2: I need to display information here about the subitem
Which I can't find a way to pass it to the partial, or work in
this manner for existing items
-->
<%= subitem.food.name %>
<%= subitem.food.calories %>
<%= f.fields_for subitem, :index => subitem do |line_item_form| %>
<%= line_item_form.label :servings %><br/>
<%= line_item_form.text_field :servings %><br/>
<%= line_item_form.label :food_id %><br/>
<%= line_item_form.text_field :food_id %><br/>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
This works great, except, when I look at the HTML, it's creating the inputs that look like the following, failing to input the correct id and instead placing the memory representation(?) of the model. As a result, an update fails:
<input type="text" value="2" size="30" name="meal[meal_line_item][#<MealLineItem:0x00000005c5d618>][servings]" id="meal_meal_line_item_#<MealLineItem:0x00000005c5d618>_servings">
EDIT:
The reason I'm attempting to do it in this method is that I need to gather some information on associations for existing meal_line_items. For example, in the area where I took out code, I have some code to the effect of:
<%= subitem.food.name %>
<%= subitem.food.calories %>
Getting this information won't work if I am using a form builder with partials, at least, not in my trials.
Edit 2:*
See the edit in the code. Here's my MealLineItem
class MealLineItem < ActiveRecord::Base
# Associations ---------------------
belongs_to :food
belongs_to :meal
end
And meal accepts_nested_attributes for the model. As you can see it belongs to both food and meal model. For the existing meal_line_item I need to do something like:
meal_line_item.food.name
Is there f. missing from <%= fields_for?
--edit
Have you tried:
<%= f.fields_for 'meal[meal_line_item][]', subitem do |line_item_form| %>
--edit
Docs say that it should work without loop too:
<%= form_for(#meal) do |f| %>
<!-- some other stuff that's irrelevant... -->
<%= f.fields_for :meal_line_items do |line_item_form| %>
<%= line_item_form.label :servings %><br/>
<%= line_item_form.text_field :servings %><br/>
<%= line_item_form.label :food_id %><br/>
<%= line_item_form.text_field :food_id %><br/>
<% end %>
<%= f.submit %>
<% end %>
Have to test this but maybe this approach?
_form
<%= form.fields_for :meal_line_items do |meal_line_item_form| %>
<% #meal.meal_line_items.each do |meal_line_item| %>
<%= render :partial => "meal_line_items/meal_line_item", :locals => { :meal_line_item_form => meal_line_item_form, :meal_line_item => meal_line_item } %>
<% end %>
<% end %>
meal_line_items/_meal_line_item.erb
<%= meal_line_item_form.label :servings %><br/>
<%= meal_line_item_form.text_field :servings %><br/>
<%= meal_line_item_form.label :food_id %><br/>
<%= meal_line_item_form.text_field :food_id %><br/>
EDIT
here's a link to an example for setting the formbuilder iterator directly (Rails 2.3.8 though). The associations between Outbreak -> Incidents -> Location should be similiar to the ones for Meal -> Meal_line_items -> Food.
AJAX update of accepts_nested_attributes_for partials
After searching high and low, I found the error. Although I was modifying the partial and was receiving a NameError it's because I was calling the partial from a helper method - exactly the same problem as stated in the following question:
rails fields_for render partial with multiple locals producing undefined variable

Resources