私はLikesとDis-likesの機能を追加することに苦労しています。私はルートを入れ子にしていると私は私が間違っているのかを把握カント:未初期化定数CreateCampaigns :: LikeController
私のルートは、私のようController.rbファイル
のコントローラapp/controllers/createcampaigns/likes_controller.rb
コンテンツのように、この
createcampaign_like_index GET /createcampaigns/:createcampaign_id/like(.:format) createcampaigns/like#index
POST /createcampaigns/:createcampaign_id/like(.:format) createcampaigns/like#create
new_createcampaign_like GET /createcampaigns/:createcampaign_id/like/new(.:format) createcampaigns/like#new
edit_createcampaign_like GET /createcampaigns/:createcampaign_id/like/:id/edit(.:format) createcampaigns/like#edit
createcampaign_like GET /createcampaigns/:createcampaign_id/like/:id(.:format) createcampaigns/like#show
PATCH /createcampaigns/:createcampaign_id/like/:id(.:format) createcampaigns/like#update
PUT /createcampaigns/:createcampaign_id/like/:id(.:format) createcampaigns/like#update
DELETE /createcampaigns/:createcampaign_id/like/:id(.:format) createcampaigns/like#destroy
resources :createcampaigns do
resources :like, module: :createcampaigns
のように見えます
class Createcampaigns::LikesController < ApplicationController
before_action :authenticate_user!
def likes
end
def index
@createcampaign = Createcampaign.find(params[:createcampaign_id])
@createcampaign.likes.where(user_id: current_user.id).first_or_create
respond_with(@createcampaign)
end
def create
@createcampaign.like.where(user_id: current_user.id).first_or_create
respond_to do |format|
format.html { redirect_to @createcampaign }
format.js
end
end
private
def set_post
@createcampaign = Createcampaign.find(params[:createcampaign_id])
end
end
app/views/createcampaigns/show.html.erb
<div id="likes">
<%= render partial: "likes" %>
</div>
Iがリンクとしてこれを最初の試みユーザモデル
has_many :likes
def likes?(post)
post.likes.where(user_id: id).any?
end
:
<%= link_to "Like", createcampaign_like_path(@createcampaign), method: :like %>
を私は(回線#2の周りに)抽出されたソース
ActionController::UrlGenerationError in Createcampaigns#show
Showing /home/ubuntu/workspace/app/views/createcampaigns/_like.html.erb where line #2 raised:
No route matches {:action=>"show", :controller=>"createcampaigns/like", :createcampaign_id=>#<Createcampaign id: 127, name: "Sessions must go; special prosecutor must be appoi...", description: "Attorney General Jeff Sessions lied under oath abo...", image: nil, location: "35 windfield ave", reason: "During his confirmation hearing, when asked by Sen...", bio: nil, user_id: 18, policy_in_question: nil, time: nil, contact_info: nil, city: "jersey city", state: "nj", zipcode: "07521", address: "3", country: "USA", banner: nil, created_at: "2017-03-15 14:44:04", updated_at: "2017-03-16 12:24:10", campaign_image: nil, campaignimage: "americanflag.jpg", eventgroundrules: nil, starts_at: nil, start_time: "4pm", campaigndate: "2017-03-15", campaigntarget: 0, admin: true, category_name: nil, category_id: 6, president_id: 743, senatemember_id: 1819, housemember_id: 1408>, :id=>nil} missing required keys: [:id]
このエラーを取得します:
ので、私は、ユーザーIDを渡すと
<%= link_to "Like", createcampaign_like_path(user_id: current_user.id), method: :like %>
しようとした私は、この得、それは属性を必要とするか、または欠落しているようだ:私のルートはどうあるべき
No route matches {:action=>"show", :controller=>"createcampaigns/like", :id=>"127", :user_id=>16} missing required keys: [:createcampaign_id]
Extracted source (around line #2):
1
2
<%= link_to "Like", createcampaign_like_path(user_id: current_user.id), method: :like %>
を?助けてください..レールに新
資源:好き、モジュール:::createcampaigns リソース:コメントは、モジュール:createcampaigns リソース:コメントは、モジュール:除き、鳴き:[:ショー、:インデックス] リソースを悲鳴createcampaignsが リソースを行います メンバーが GETを実行します。フォローし エンド エンド –