我是一名ROR新手,而且我在某种程度上打破了我应用中的创建和编辑功能。我为每个上传过的艺术品都有单独的页面,一个能够编辑这些艺术品的形式(以及它们的原创作品),以及两个不同的索引页面:一个非常漂亮的索引页面(在应用程序上#39) ; s主页),这是一张只包含每件艺术品(后端)所有信息的大表。
的routes.rb
Rails.application.routes.draw do
root "pages#home"
get "artworks" => "artworks#index"
# post "artworks" => "artworks#index"
resources :artworks, path: ''
get "about" => "pages#about"
end
artworks_controller.rb
class ArtworksController < ApplicationController
before_action :set_artwork, only: [:show, :edit, :update, :destroy]
# GET /artworks
# GET /artworks.json
def index
@artworks = Artwork.all
end
# GET /artworks/1
# GET /artworks/1.json
def show
@artwork = Artwork.friendly.find(params[:id])
if request.path != artwork_path(@artwork)
redirect_to @artwork, status: :moved_permanently
end
end
# GET /artworks/new
def new
@artwork = Artwork.new
end
# GET /artworks/1/edit
def edit
end
# POST /artworks
# POST /artworks.json
def create
@artwork = Artwork.new( artwork_params )
respond_to do |format|
if @artwork.save
format.html { redirect_to @artwork, notice: 'Artwork was successfully created.' }
format.json { render :show, status: :created, location: @artwork }
else
format.html { render :new }
format.json { render json: @artwork.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /artworks/1
# PATCH/PUT /artworks/1.json
def update
respond_to do |format|
if @artwork.update(artwork_params)
format.html { redirect_to @artworks, notice: 'Artwork was successfully updated.' }
format.json { render :show, status: :ok, location: @artwork }
else
format.html { render :edit }
format.json { render json: @artwork.errors, status: :unprocessable_entity }
end
end
end
# DELETE /artworks/1
# DELETE /artworks/1.json
def destroy
@artwork.destroy
respond_to do |format|
format.html { redirect_to artworks_url, notice: 'Artwork was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_artwork
@artwork = Artwork.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def artwork_params
params.require(:artwork).permit(:title, :image, :genre, :category, :medium, :slug, :availability, :date, :height, :width)
end
end
pages_controller.rb
class PagesController < ApplicationController
def home
@artworks = Artwork.all
end
def about
end
end
index.html.rb(在views / artwork中)
<div class="row">
<div class="col-xs-6 col-sm-8 col-md-4">
<h1>Listing artworks</h1>
</div>
<div class="col-xs-4 col-sm-2 col-md-2 col-md-offset-6">
<%= link_to 'New Artwork', new_artwork_path, class:"btn btn-default" %>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Image</th>
<th>Title</th>
<th>Genre</th>
<th>Category</th>
<th>Medium</th>
<th>Date</th>
<th>Dimensions</th>
<th>Availability</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<% @artworks.each do |artwork| %>
<tr>
<td><%= link_to image_tag(artwork.image.url(:thumb)), artwork %></td>
<td><%= artwork.title %></td>
<td><%= artwork.genre %></td>
<td><%= artwork.category %></td>
<td><%= artwork.medium %></td>
<td><%= artwork.date %></td>
<td><%= artwork.height %> x <%= artwork.width %></td>
<td><%= artwork.availability %> </td>
<td><%= link_to 'Edit', edit_artwork_path(artwork) %></td>
<td></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<br>
_form.html.rb
<%= simple_form_for @artwork, :html => {:multipart => true } do |f| %>
<% if @artwork.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@artwork.errors.count, "error") %> prohibited this artwork from being saved:</h2>
<ul>
<% @artwork.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="col-md-4">
<%= f.input :image %>
</div>
<div class="col-md-8">
<%= f.input :title %>
</div>
</div>
<div class="row">
<div class="col-md-4">
<%= f.input :genre, :collection => ['Landscape', 'Still life', 'Figure', 'Interior'], :include_blank => false, :as => :radio_buttons %>
</div>
<div class="col-md-4">
<%= f.input :category, :collection => ['Painting', 'Drawing', 'Sculpture', 'Print', 'Mixed media'], :include_blank => false, :as => :radio_buttons %>
</div>
<div class="col-md-4">
<%= f.input :medium, :collection => ['Oil on paper', 'Oil on canvas', 'Oil on canvas mounted on panel', 'Watercolor', 'Ink wash', 'Charcoal', 'Graphite', 'Oil pastel'] %>
</div>
</div>
<div class="row">
<div class="col-md-4">
<%= f.input :date, :include_blank => true, as: :string, start_year: Time.now.year - 100, end_year: Time.now.year, order: [ :year, :month, :day,], hint: 'Order: Year, month, date' %>
</div>
<div class="col-md-4">
<%= f.input :height %>
<%= f.input :width %>
</div>
<div class="col-md-4">
<%= f.input :availability, :collection => ['Available for purchase','Private collection','Not for sale'], :as => :radio_buttons %>
</div>
</div>
<div class="row">
<div class="col-md-8">
<%= f.input :notes %>
</div>
</div>
<div class="row">
<div class="col-md-2 col-md-offset-10">
<%= f.button :submit, class: "btn btn-primary" %>
</div>
</div>
<!--
<div class="form-group">
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
<div class="form-group">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="form-group">
<%= f.label :genre %><br>
<%= f.select :genre, ['Landscape', 'Still life', 'Figure', 'Interior'] %>
</div>
<div class="form-group">
<%= f.label :category %><br>
<%= f.select :category, ['Painting', 'Drawing', 'Sculpture', 'Print', 'Mixed media'] %>
</div>
<div class="form-group">
<%= f.label :medium %><br>
<%= f.select :medium, ['Oil on paper', 'Oil on canvas', 'Oil on canvas mounted on panel', 'Watercolor', 'Ink wash', 'Charcoal', 'Graphite'] %>
</div>
<div class="actions">
<%= f.submit %>
</div> -->
<% end %>
和home.html.erb(在views / pages中)
<div id="artworks" class="transitions-enabled">
<% @artworks.each do |artwork| %>
<div class="box">
<%= link_to image_tag(artwork.image.url(:medium)), artwork %>
</div>
<% end %>
</div>
model:artwork.rb
class Artwork < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
extend FriendlyId
friendly_id :slug_candidates, use: [:slugged, :history]
def slug_candidates
[
:title,
[:title, :genre],
[:title, :genre, :category],
[:title, :genre, :category, :medium]
]
end
def previous
Artwork.where(["id < ?", id]).order('id').last
end
def next
Artwork.where(["id > ?", id]).first
end
end
我使用了一些我喜欢的宝石(friendly_id),但不知怎的,我已经破坏了表单,因为我在ArtworksController#update中得到了错误&#34; ActionController :: ActionControllerError 无法重定向到nil!&#34;与第49行有关:
format.html { redirect_to @artworks, notice: 'Artwork was successfully updated.' }
当我创建新的艺术作品时,我看到了这个错误:&#34;路由错误 没有路线匹配[POST]&#34; / artwork&#34;&#34;
rake routes:
Prefix Verb URI Pattern Controller#Action
artworks GET /artworks(.:format) artworks#index
root GET / pages#home
GET / artworks#index
POST / artworks#create
new_artwork GET /new(.:format) artworks#new
edit_artwork GET /:id/edit(.:format) artworks#edit
artwork GET /:id(.:format) artworks#show
PATCH /:id(.:format) artworks#update
PUT /:id(.:format) artworks#update
DELETE /:id(.:format) artworks#destroy
about GET /about(.:format) pages#about
任何人都可以帮我看看我做错了什么吗?提前谢谢!
答案 0 :(得分:0)
替换
redirect_to @artworks
到
redirect_to artworks_url
还修改routes.rb
Rails.application.routes.draw do
root "pages#home"
resources :artworks
get "about" => "pages#about"
end
答案 1 :(得分:0)
我通过做一些事情来解决这个问题:
重写路线如下:
root“艺术品#index” 得到“约”=&gt; “网页#约” 得到“admin”=&gt; “艺术品#管理员”
This other thread I posted帮我解决了这个问题。 @Ajay和@ Deep的回答帮助我思考了这一点。