ruby - Rails How to pass params in cross model system -
i unable figure out method pass params link tag. want when clicks on join group link, membership model shall have new row group_id current group id , user id current user id. membership model consists of 2 columns : user_id , group_id maps users groups. can me mistake making. here code groups : show.html.erb
<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 %>
and here memberships controller
class membershipscontroller < applicationcontroller before_action :set_membership, only: [:show, :edit, :update, :destroy] # /memberships # /memberships.json def index @memberships = membership.all end # /memberships/1 # /memberships/1.json def show @membership = membership.find(params[:id]) @user = user.find(@membership.user_id) @group = group.find(@membership.group_id) end # /memberships/new def new @membership = membership.new end # /memberships/1/edit def edit end # post /memberships # post /memberships.json def create @membership = membership.new(membership_params) respond_to |format| if @membership.save format.html { redirect_to @membership, notice: 'membership 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 |format| if @membership.update(membership_params) format.html { redirect_to @membership, notice: 'membership 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 |format| format.html { redirect_to memberships_url, notice: 'membership destroyed.' } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_membership @membership = membership.find(params[:id]) end # never trust parameters scary internet, allow white list through. def membership_params params.require(:membership).permit(:user_id, :group_id) end end
you should have method in controller handles adding user group, like:
class membershipscontroller < applicationcontroller ... def join_group @user = user.find(params[:user] @user.group_id = params[:group_id].to_i end ... end
then routes.rb needs path controller method:
get 'memberships/join_group' => 'memberships#join_group'
which result in path memberships_join_group_path
use in link_to:
<%= link_to 'join group', memberships_join_group_path(:group_id => @group.id, :user_id => current_user.id ) %>
the path you're using sending params index
method.
Comments
Post a Comment