How to get a nested text_area to render properly - ruby-on-rails

I have a scaffold called submits that has a form for creating new submissions. I also created a model called question. I've used this model to create different questions in the submission form.I've utilized a join form and use active admin to add/edit questions from the backend. I'm getting this error.
undefined method `submit[question_ids][]' for #<Submit id: nil, name: nil, created_at: nil, updated_at: nil>
submits.rb
class Submit < ActiveRecord::Base
has_and_belongs_to_many :questions
end
question.rb
class Question < ActiveRecord::Base
has_and_belongs_to_many :submits
end
subits/_form.html.erb
<%= form_for(#submit) do |f| %>
<% if #submit.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#submit.errors.count, "error") %> prohibited this submit from being saved:</h2>
<ul>
<% #submit.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<div class="field">
<%= f.label :name,"Team Name" %><br>
<%= f.text_field :name, class: "form-control" %>
</div>
<% #questions.each do |question| %>
<div class="field">
<%= f.label(question.question)%>
<%= f.text_area "submit[question_ids][]" %>
</div>
<% end %>
<div class="actions">
<%= f.submit "Apply", class: "btn btn-primary btn-lg" %>
</div>
<% end %>
I'm guessing my error is here:
<%= f.text_area "submit[question_ids][]" %>
I'm just not sure what the correct syntax is. Any suggestions?

For Rails form_for input forms, you need :(whatever attribute) which should be defined in your migration file.
ex.) if you have 'text' attribute in your Submit model you can have your input form for the text attribute just like this. <%= f.text_area :text %>
But in this case it seems like you have a join table for your models, so I think you should fields_for for your join table.
cf.) How do i include Rails join table field in the form?

Related

Nested forms rails field does not update

I am trying to do nested forms like mentioned here.
http://guides.rubyonrails.org/form_helpers.html#nested-forms
The goal is as follows: I have multiple colli with one checkbox which can be checked. The colli list can be deleted or modified but the checks and their information need to stay.
Model
class Colli < ActiveRecord::Base
has_one :check, foreign_key: "subcontainerid", primary_key: "colliid"
accepts_nested_attributes_for :check, allow_destroy: true
end
class Check < ActiveRecord::Base
belongs_to :colli
end
So every colli has one check. The colliid from the colli table created a link with the check table using the subcontainer id.
Controller
Within the colli controller I whitelist the check_attributes.
def colli_params
params.require(:colli).permit(:colliid, :collinaam, check_attributes: [:id, :checked])
end
Form
My form looks like this.
<%= form_for(#colli) do |f| %>
<% if #colli.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#colli.errors.count, "error") %> prohibited this colli from being saved:</h2>
<ul>
<% #colli.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.fields_for :checks do |checks_f| %>
<p>check start</p>
<div class="field">
<%= checks_f.label :checked %><br>
<%= checks_f.check_box :checked %>
</div>
<% end %>
<div class="field">
<%= f.label :colliid %><br>
<%= f.text_field :colliid %>
</div>
<div class="field">
<%= f.label :collinaam %><br>
<%= f.text_field :collinaam %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
If I do form_for :check I can't see the checkboxes. When I do form_for :checks I see a checkbox but it does not work. When clicking submit I see following error:
undefined method `checked' for nil:NilClass
<p>
<strong>Checked:</strong>
<%= #colli.check.checked %>
</p><p>
<strong>Collinaam:</strong>
<%= #colli.collinaam %>
Which means it did not get saved. Does anybody know how to fix this?
Try adding this to your form-
<%= f.fields_for :checks, #colli.check.build do |checks_f| %>
<p>check start</p>
<div class="field">
<%= checks_f.label :checked %><br>
<%= checks_f.check_box :checked %>
</div>
<% end %>

Rendering a form that creates an object that is Nested under two other models.

I'm building a rails app that has a an object that is created /nested underneath two objects.
Routes.rb
resources :pages do
resources :referralpages do
resources :rewards do
end
end
end
I've just added the rewards resource and have this for my form for creating a new reward
<%= form_for ([#referralpage, #reward]) do |f| %>
<% if #reward.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#reward.errors.count, "error") %> prohibited this reward from being saved:</h2>
<ul>
<% #reward.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %><br>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :level %><br>
<%= f.text_field :level, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :dscount %><br>
<%= f.text_field :discount, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit class: "btn btn-primary" %>
</div>
<% end %>
I need help getting the form_for ([#referralpage, #reward]) portion working.
Here's the error message I'm getting when clicking the new reward button:
undefined method `referralpage_rewards_path'
<%= form_for ([#referralpage, #reward]) do |f| %>
my guess is that it's routing to the incorrect path. What's the proper syntax to get this to work?
I think it should render this path
new_page_referralpage_reward
The point of this feature is to render rewards in the referral/show.html.erb page
I have created an index partial in my rewards view file and am rendering it in the show action of the referralpages/show.html.erb file.
I think you cannot put more than one resource path in form_for which cause invalid path.
Why you want to put 2 resource path in form?
Do you plan to save the same data for referral & rewards Model?
If, yes use just one path and make a create method in your controller to save to other model.
From this point of view:
The point of this feature is to render rewards in the
referral/show.html.erb page
If you only plan to render the data of rewards to referral/show.html.erb,
in your referral controller
def show
#rewards = Reward.all #example
end
Unless, you have model relationships like:
#Reward Model
belongs_to :refferal
#Referral Model
has_many :rewards or has_one :reward
With model realtionship:
def show
#referal = Referral.all #example
end
show.html.erb View # iterate it:
<%for referral in #referral%>
<% referral.rewards %> # need to iterate if has many
or
<%= referral.reward... %>
<%end%>

What Is The Best Method To Create Edit Forms?

I'm working on an application in Ruby on Rails (Ruby 2 - Rails 4 - Bootstrap 3)
I have used the simple_form gem to build the forms, like signup and register, but how do you create a form that loads an object from the database and allows a user to edit the details?
Say we had a Product table in the database and I wanted to create a form to load the details of that product into the form and allow the user to edit a product's description, price, etc.
I have had a look around but still not clear.
Thanks.
First you need to put a link from your view to the edit action where you send the product as a parameter this usually goes in your index (app/views/products/index.html.erb). It should look something like this:
<%= link_to 'Edit', edit_product_path(product) %>
Then you need to make sure you have the edit action in your Products controller (app/controllers/products_controller.rb):
def edit
end
Now your edit.html.erb (app/views/products/edit.html.erb) should look something like this:
<h1>Editing product</h1>
<%= render 'form' %>
<%= link_to 'Show', #product %> |
<%= link_to 'Back', product_path %>
And finally the form you are rendering that should be located in app/views/_form.html.erb should look like this:
<%= form_for(#product) do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :descriptions %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Tip: When you generate a Scaffold with the rails generate Scaffold command, it automatically creates the edit, delete, show and new actions for your model and all the views and classes I mentioned above.
rails generate Scaffold Product name:string description:text price:decimal
Hope it helps!

Rails select NoMethodError in Messages#new

I'm trying to put on a select box the Users who will receive the message.
What we have is one table named Messages, one named Users and one named Friendships, on message table we have user_id (sender) and friend_id (receiver) both foreign key to Users ID.
And this is the error returning: undefined method `friend_id' for #Message:0x56f6238
That been said, I'm trying to vinculate all Friendships entries with current_user id and then using friend_id to search on Users table all users that current_user is able to send message.
Any help with this error? I'm really new on Ruby On Rails development.
If I'm missing some info, let me know.
Thanks in advance
On our application, the new_message_path renders one partial which contains the following:
<%= form_for(#message) do |f| %>
<% if #message.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#message.errors.count, "error") %> prohibited this message from being saved:</h2>
<ul>
<% #message.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Receiver" %><br />
<%= f.select :friend_id, Message.all.collect {|friendships| [ friendships.friend_id ] }, { include_blank: true } %>
</div><br>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And on our model named Message we have this:
class Message < ActiveRecord::Base
attr_accessible :content, :friend_id
validates_presence_of :content, :friend_id, presence => true
belongs_to :user
end

Passing User_ID through new_item form

I have a simple app that allows users to create 'items'. On the _form, the only data that it asks for is 'content' and 'user_id', which is currently a number picker that assigns user_id to the item for ownership. But what I want to do is have the form assume that the user_id is the current user's ID (using Devise). That way other people can't assign 'items' to other users. Make sense? Here's the form.
_form.html.erb
<%= form_for(#item) do |f| %>
<% if #item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul>
<% #item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You should be working with associations from the model.
example your Items model should have belongs_to :user
then your would just use the :user method not the user_id attribute.
But you really could make this much more simpler.
install simple_forms gem it will make your life easier.
gem "simple_form"
then
<%= simple_form_for(#item) do |f| %>
<%= f.input :content %>
<%= f.association :user %>
<%= f.button :submit %>
<% end %>

Resources