我为我的模型"Things"创建了一个编辑表单,但是当我提交表单时,它会返回错误"No route matches [PATCH]"/ things"".奇怪的是,错误的网址是http://localhost:3000/things,但我期待它http://localhost:3000/things/:id,因为我在create function下编写了redirect_to @thing.我刚刚用资源定义了所有我的Thing路由:事情,所以它应该定义更新路径.我究竟做错了什么?
things_controller:
class ThingsController < ApplicationController
def show
@thing = Thing.find(params[:id])
@category_things = CategoryThing.all
@thing.categories.build
@thing.things.build
@related_things = RelatedThing.all
end
def index
end
def new
@thing = Thing.new
@things = Thing.all
end
def create
@thing = Thing.new(thing_params)
if @thing.save
redirect_to @thing
else
render 'new'
end
end
def edit
@thing = Thing.find(params[:id])
end
def update
@thing = Thing.find(params[:id])
if @thing.update_attributes(thing_params)
flash[:success] = "Thing updated"
redirect_to @thing
else
render 'edit'
end
end …Run Code Online (Sandbox Code Playgroud) Rails中有一种方法可以在提交表单时发送自动电子邮件,其中包含该表单中的信息吗?具体来说,我希望新用户在提交注册表单时收到包含他的新个人资料网址的电子邮件.
这是我的表单的样子:
<h1>Add Something!</h1>
<p>
<%= form_for @thing, :url => things_path, :html => { :multipart => true } do |f| %>
<%= f.text_field :name, :placeholder => "Name of the thing" %>
<%= f.label :display_picture %>
<%= f.file_field :avatar %>
<br>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</p>
Run Code Online (Sandbox Code Playgroud)
控制器:
class ThingsController < ApplicationController
def show
@thing = Thing.find(params[:id])
end
def new
@thing = Thing.new
@things = Thing.all
end
def create
@thing = Thing.new(thing_params)
if @thing.save
render :action …Run Code Online (Sandbox Code Playgroud) 不是骗局.我在问题的底部添加了原因.
我有两个模型,Playlist并且User通过第三个表格相互拥有和相互拥有PlaylistUser.
对于某个播放列表@playlist,我想打印与用户共享的所有其他播放列表的列表@playlist.理想情况下,此列表将按其共享的用户数量排序.
此查询旨在执行此操作,但会导致错误:
Playlist.joins(:users).where.not(id: @playlist.id).where(users: {id: @playlist.user_ids}).group(:id).order('count(*) desc')
Run Code Online (Sandbox Code Playgroud)
模型
class Playlist < ActiveRecord::Base
has_many :playlist_users
has_many :users, :through => :playlist_users
end
class PlaylistUser < ActiveRecord::Base
belongs_to :playlist
belongs_to :user
end
class User < ActiveRecord::Base
has_many :playlist_users
has_many :playlists, :through => :playlist_users
end
Run Code Online (Sandbox Code Playgroud)
我认为问题是Rails不知道:id我指的是哪一列.
我该如何解决这个问题?
这是我在控制台中运行查询时得到的错误:
> @playlist = Playlist.first
> Playlist.joins(:users).where.not(id: @playlist.id).where(users: {id: @playlist.user_ids}).group(:id).order('count(*) desc')
(415.0ms) SELECT "users".id FROM "users" INNER JOIN "playlist_users" ON "users"."id" = "playlist_users"."user_id" …Run Code Online (Sandbox Code Playgroud) jsonlint.com 在我的第二行代码中抛出以下错误:“SyntaxError:第 2 行错误字符串”
但是无论我用什么字符串替换当前的字符串,它都会不断抛出该错误。它指的是“1”以外的其他东西吗?
{
"1": [{
"city": "Paris",
"country": "France",
"date": 2015,
"sights": [{"Eiffel Tower", "Versailles"}]
}],
"2": [{
"city": "Rome",
"country": "Italy",
"date": 2016,
"sights": [{"Parthenon"}]
}]
}
Run Code Online (Sandbox Code Playgroud)