Created
November 20, 2013 13:53
-
-
Save salami-art/7563520 to your computer and use it in GitHub Desktop.
Want to create a new author using "Author" field in the form in books/new, if it doesn't exist yet. The code provided in _form is not working. Routes autor/new, author/create are available.
This file contains hidden or 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
<%= form_for([@author, @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 |msg| %> | |
<li><%= msg %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
<ul> | |
<li>Title <br> <%= f.text_field :title %></li> | |
<li>Author<br> <%= f.text_field :name %></li> | |
<li>Text <br> <%= f.text_area :text %></li> | |
</ul> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> |
This file contains hidden or 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
class Book < ActiveRecord::Base | |
# set_root_path Rails.root.join('config') | |
# set_filename 'books' | |
validates :title, :presence => true | |
validates :author_id, :presence => true | |
validates :text, :presence => true | |
before_create :fix_title | |
belongs_to :author | |
def fix_title | |
self.title = self.title.titleize | |
end | |
end |
This file contains hidden or 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
class BooksController < ApplicationController | |
class Author < ActiveRecord::Base | |
validates :name, :presence => true | |
has_many :books | |
end | |
def index | |
@books = Book.all | |
respond_to do |format| | |
format.html | |
format.yaml { render text:@books.to_yaml } | |
format.json { render text:@books.to_json } | |
end | |
end | |
def show | |
@book = Book.find(params[:id]) | |
respond_to do |format| | |
format.html | |
format.yaml { render text:@book.to_yaml } | |
end | |
end | |
def new | |
@book = Book.new | |
end | |
def create | |
@book = Book.new(book_params) | |
if @book.save | |
redirect_to book_path(@book) | |
else | |
render :new | |
end | |
end | |
def edit | |
@book = Book.find(params[:id]) | |
end | |
def update | |
@book = Book.find(params[:id]) | |
end | |
private | |
def book_params | |
params.require(:book).permit(:title, :text, :author_id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bookstore::Application.routes.draw do
The priority is based upon order of creation: first created -> highest priority.
See how all your routes lay out with "rake routes".
resources :books
resources :authors, :only => [:index, :create]
You can have the root of your site routed with "root"
root 'books#index'
Example of regular route:
get 'products/:id' => 'catalog#view'
Example of named route that can be invoked with purchase_url(id: product.id)
get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
Example resource route (maps HTTP verbs to controller actions automatically):
resources :products
Example resource route with options:
resources :products do
member do
get 'short'
post 'toggle'
end
collection do
get 'sold'
end
end
Example resource route with sub-resources:
resources :products do
resources :comments, :sales
resource :seller
end
Example resource route with more complex sub-resources:
resources :products do
resources :comments
resources :sales do
get 'recent', on: :collection
end
end
Example resource route with concerns:
concern :toggleable do
post 'toggle'
end
resources :posts, concerns: :toggleable
resources :photos, concerns: :toggleable
Example resource route within a namespace:
namespace :admin do
# Directs /admin/products/* to Admin::ProductsController
# (app/controllers/admin/products_controller.rb)
resources :products
end
end