我無法找出將params傳遞給鏈接標記的方法。我想要做的是當有人點擊加入組鏈接時,會員模型應該有一個新的行,其group_id作爲當前組ID,用戶ID作爲當前用戶ID。成員資格模型目前由兩列組成:user_id和group_id將用戶映射到組。任何人都可以幫我解決我犯的錯誤。 下面是代碼 組:show.html.erbRails如何在跨模型系統中傳遞參數
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @group.title %>
</p>
<p>
<strong>Desc:</strong>
<%= @group.desc %>
</p>
<p>
<strong>Creator:</strong>
<%= @creator.first_name %>
</p>
<%= link_to 'Join Group', memberships_path(:group_id => @group.id, :user_id => current_user.id), method: :post %>
<%= link_to 'Edit', edit_group_path(@group) %> |
<%= link_to 'Back', groups_path %>
這裏是成員控制器
class MembershipsController < ApplicationController
before_action :set_membership, only: [:show, :edit, :update, :destroy]
# GET /memberships
# GET /memberships.json
def index
@memberships = Membership.all
end
# GET /memberships/1
# GET /memberships/1.json
def show
@membership = Membership.find(params[:id])
@user = User.find(@membership.user_id)
@group = Group.find(@membership.group_id)
end
# GET /memberships/new
def new
@membership = Membership.new
end
# GET /memberships/1/edit
def edit
end
# POST /memberships
# POST /memberships.json
def create
@membership = Membership.new(membership_params)
respond_to do |format|
if @membership.save
format.html { redirect_to @membership, notice: 'Membership was successfully created.' }
format.json { render :show, status: :created, location: @membership }
else
format.html { render :new }
format.json { render json: @membership.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /memberships/1
# PATCH/PUT /memberships/1.json
def update
respond_to do |format|
if @membership.update(membership_params)
format.html { redirect_to @membership, notice: 'Membership was successfully updated.' }
format.json { render :show, status: :ok, location: @membership }
else
format.html { render :edit }
format.json { render json: @membership.errors, status: :unprocessable_entity }
end
end
end
# DELETE /memberships/1
# DELETE /memberships/1.json
def destroy
@membership.destroy
respond_to do |format|
format.html { redirect_to memberships_url, notice: 'Membership was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_membership
@membership = Membership.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def membership_params
params.require(:membership).permit(:user_id, :group_id)
end
end
但這不能將其存放於會員數據庫。 –
這不是你要解決的問題嗎?你說你的問題是通過link_to傳遞params,我回答了。如果你不能在你的join_group方法中提出邏輯,這是一個單獨的問題。 – MarsAtomic
對不起,我對rails比較陌生。我試圖def join_group @user = User.find(current_user.id) Membership.new(user_id:current_user.id,group_id:@group.id) Membership.save end –