突然间,即使它已经工作了一段时间,我也会收到此错误:
没有路线匹配[POST]“/ stories / id / invites”
路线:
story_invites_path GET /stories/:story_id/invites(.:format) invites#index
的routes.rb
resources :stories do
match '/stories/:id/invite', to: 'invite#show', via: 'get'
resources :invites , only: [:index, :show, :destroy ]
end
邀请/索引视图
<%= form_for @invite , :url => story_invites_path do |f2| %>
<%= f2.text_field :user_id, :value => user.id , :class => 'number_field' %>
<%= f2.submit 'Send', class: 'btn btn-primary' %>
<% end %>
邀请控制器
def create
@invite = @story.invites.new(invite_params)
if @invite.save
flash[:success] = 'The user was invited!'
redirect_to(:back)
else
render :new
end
end
如何在邀请路径中添加帖子方法?
答案 0 :(得分:1)
你有各种各样的东西......
这:
resources :stories do
match '/stories/:id/invite', to: 'invite#show', via: 'get'
resources :invites , only: [:index, :show, :destroy ]
end
应该是:
resources :stories do
resources :invites
end
哪个会给你:
story_invites GET /stories/:story_id/invites(.:format) invites#index
POST /stories/:story_id/invites(.:format) invites#create
new_story_invite GET /stories/:story_id/invites/new(.:format) invites#new
edit_story_invite GET /stories/:story_id/invites/:id/edit(.:format) invites#edit
story_invite GET /stories/:story_id/invites/:id(.:format) invites#show
PATCH /stories/:story_id/invites/:id(.:format) invites#update
PUT /stories/:story_id/invites/:id(.:format) invites#update
DELETE /stories/:story_id/invites/:id(.:format) invites#destroy
stories GET /stories(.:format) stories#index
POST /stories(.:format) stories#create
new_story GET /stories/new(.:format) stories#new
edit_story GET /stories/:id/edit(.:format) stories#edit
story GET /stories/:id(.:format) stories#show
PATCH /stories/:id(.:format) stories#update
PUT /stories/:id(.:format) stories#update
DELETE /stories/:id(.:format) stories#destroy
如果你真的想限制你的invite
路线,那很好。但是现在,你正在限制create
。哪个真的为什么你会得到:
No route matches [POST] “/stories/id/invites”
因为你玩过Tails rails,所以你不想要创建路径。 Rails就这么愚蠢。你说的是什么不是你的意思。
另外,这个:
match '/stories/:id/invite', to: 'invite#show', via: 'get'
真的充满了不良和香蕉slu ..请不要这样做。对自己。或者对我们来说。
此:
<%= form_for @invite , :url => story_invites_path do |f2| %>
<%= f2.text_field :user_id, :value => user.id , :class => 'number_field' %>
<%= f2.submit 'Send', class: 'btn btn-primary' %>
<% end %>
可能需要看起来更像这样:
<%= form_for :invite, story_invites_path(story) do |f2| %>
<%= f2.hidden_field :invite, :user_id, value: @user.id %>
<%= f2.submit 'Send', class: 'btn btn-primary' %>
<% end %>
使用:invite
代替@invite
,因为您已index
,而您可能没有@invite
。如果你这样做,那很好。但是rails可以使用符号来推断出正确的字段。
您必须look up使用hidden_field
方法,因为我是从记忆中做到这一点的。
而且我不知道你将story
从哪里获得,因为你在index
方法中。但是,你可以做@stories = Story.all
之类的事情。在这种情况下,整个块需要包含在以下内容中:
<% @stories.each do |story| %>
<%= story.name %>
<%= form_for :invite, story_invites_path(story) do |f2| %>
<%= f2.hidden_field :invite, :user_id, value: @user.id %>
<%= f2.submit 'Send', class: 'btn btn-primary' %>
<% end %>
<% end %>
伙计,这让我想起为什么我不喜欢erb
。如果你在HAML中这样做,它会很漂亮,就像独角兽和彩虹一样:
- @stories.each do |story|
.story-container{id: "story-id-#{story.id}"}
.story-name
= story.name
.story-invite-container
= form_for :invite, story_invites_path(story) do |f2|
= f2.hidden_field :invite, :user_id, value: @user.id
= f2.submit 'Send', class: 'btn btn-primary'
你不喜欢独角兽和彩虹吗?看看所有那些美丽的divs
ids
和classes
只是等着穿着你的屁股踢,疯狂忍者技能CSS(display: inline-block
,任何人?) 。 (请告诉我你正在使用SASS。请?)
现在,在提交时,您的参数将看起来像(我正在编写story_id
和&#39; user_id`值):
{..., story_id: 4, invite: {user_id: 1}, ...}
我假设在invites_controller
中,你有类似的东西:
class InvitesController < ActionController::Base
def index
...
@stories = Story.all
...
end
def create
@story = Story.find_by(id: params[:story_id])
@invite = @story.invites.new(invite_params)
if @invite.save
flash[:success] = 'The user was invited!'
redirect_to(:back)
else
render :new
end
end
def invite_params
params.require(:invite).permit(:user_id)
end
end
这一切都应该挂在一起。
对于我的生活,我不知道你为什么redirect_to :new
,但不管你的船是什么漂浮。 (确定您不想redirect_to stories_path
或redirect_to story_path
?)
答案 1 :(得分:0)
更新您的routes.rb
以在create
中添加resources :invites
,如下所示:
resources :stories do
match '/stories/:id/invite', to: 'invite#show', via: 'get'
resources :invites , only: [:index, :show, :destroy, :create ]
end
这一变化将产生这条新路线:
story_invites POST /stories/:story_id/invites(.:format) invites#create