Last active
April 19, 2017 14:21
-
-
Save memaker/1931e71dcba0e48c477f7c11b698f186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/author.rb | |
class Author < ActiveRecord::Base | |
has_many :interests | |
end | |
# app/models/interest.rb | |
class Interest < ActiveRecord::Base | |
belongs_to :author | |
end | |
# app/controllers/authors_controller.rb | |
... | |
private | |
def set_author | |
@author = Author.find(params[:id]) | |
end | |
def author_params | |
params.require(:author).permit(:name, :interest_id) | |
end | |
... | |
# app/views/authors/_form.html.erb | |
<%= form_for(@author) do |f| %> | |
<% if @author.errors.any? %> | |
<div id="error_explanation"> | |
<h2><%= pluralize(@author.errors.count, "error") %> prohibited this author from being saved:</h2> | |
<ul> | |
<% @author.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 :author_id %><br> | |
<%= f.collection_select(:interest_id, Interest.all, :id, :name, prompt: true) %> | |
</div> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment