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
Related
In my app users can create a set of codes to used in marketing activities. Therefore I want a form in which a user can type the amount of codes he want to create and then the system should generate that many codes and save them to the database.
code.rb
class Code < ActiveRecord::Base
belongs_to :form
attr_accessor :amount
end
_form.html.erb
<%= form_for [:customer, #code] do |f| %>
<% if #code.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#code.errors.count, "error") %> prohibited this code from being saved:</h2>
<ul>
<% #code.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :amount %><br>
<%= f.text_field :amount %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
What I want to do now is to tell the controller:
take :amount
create as many SecureRandom.hex(3) as :amount specifies
save all codes to database (column for the codes in the table is named "code")
However, I totally don't know how to achieve this...
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 %>
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?
I am following my school's ruby on rails tutorial. I have followed the instructions very closely, but I have come across an error. My web interface works fine when I do localhost:3000/books (this results in me seeing my database of my book entries, which I added using the Firefox SQL manager). However, when I click "New Book" to make a new entry through the web interface, I get:
SyntaxError in BooksController#new
(NOTE: when I do localhost:3000/publisher/new, it works fine. When I do localhost:3000/authors/new, it doesn't work. I'm hoping the issue with the Author will be resolved when I fix the issue with the Book. All of the following information is about the books part of my project.)
c:/Sites/Summer_2014/BookManager/app/models/author.rb:4: syntax error, unexpected ':', expecting keyword_end validates_presence_of : :first_name, :last_name ^
This is the app trace:
app/views/books/_authors_checkboxes.html.erb:2:in `_app_views_books__authors_checkboxes_html_erb__770955296_43280532'
app/views/books/_form.html.erb:26:in `block in _app_views_books__form_html_erb___935905997_43301772'
app/views/books/_form.html.erb:1:in `_app_views_books__form_html_erb___935905997_43301772'
app/views/books/new.html.erb:3:in `_app_views_books_new_html_erb___378444391_43261320'
Here are the parts of code that I think are relevant:
app/view/books/_authors_checkboxes.html.erb:
<p>
<% for author in Author.alphabetical %>
<%= check_box_tag "book[author_ids][]", author.id, #book.authors.include?(author) %>
<%= author.name %>
<br />
<% end %>
</p>
app/views/books/_form.html.erb:
<%= form_for(#book) do |f| %>
<% if #book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% #book.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :year_published %><br>
<%= f.number_field :year_published %>
</div>
<div class="field">
<%= f.label :publisher_id %><br>
<%= f.collection_select :publisher_id, Publisher.alphabetical, :id, :name, :prompt => 'Please select a publisher' %>
</div>
<%= render partial: 'authors_checkboxes' %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
app/views/books/new.html.erb:
<h1>New book</h1>
<%= render 'form' %>
<%= link_to 'Back', books_path %>
EDIT: Here is my apps/models/author.rb:
class Author < ActiveRecord::Base
has_many :book_authors
has_many :books, through: :book_authors
validates_presence_of : :first_name, :last_name
scope :alphabetical, -> {order('last_name, first_name')}
def name
"#{last_name}, #{first_name}"
end
end
The ruby compiler is exceptionally clear on this one, it did not expect your solitary : in author.rb on line 4. It is clearly not valid ruby and you should follow the docs for validates_presence_of. The line should simply say
validates_presence_of :first_name, :last_name
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 %>