开发者

Rails (newbie) - How to store a token variable in a link in db on create action

I have a problem with my create action and I don't understand why I am wrong here.

Users can follow a link which contain a token variable. Here is my route.

routes.rb

match "/:token" => "invitations#new", :via => :get

On a create action I want the token to be stored in the sender_token instance in my db.

I tried this:

invitation controller

  def new
    @invitation = Invitation.new
  end

  def create
    @invitation = Invitation.new(params[:invitation])
    if @invitation.save

      # here the pb ???     
      @invitation.sender_token = :token

      session[:invitation] = @invitation
      Mailer.invitation(@invitation).deliver
      redirect_to invite_request_path
    e开发者_如何学JAVAlse
      render :action => 'new'
    end
  end

Thank you!


Shouldn't that be

@invitation.sender_token = params[:token]


I found this solution. Please let me know if you think it's the right way to do it!

def new
  @invitation = Invitation.new
  session[:token] = params[:token]       # or $test = params[:token] ???
end

def create
  @invitation = Invitation.new(params[:invitation])
  @invitation.sender_token = session[:token]      # or @invitation.sender_id = $test ???
    if @invitation.save
      session[:invitation] = @invitation
      Mailer.invitation(@invitation).deliver
      redirect_to invite_request_path
    else
      render :action => 'new'
    end
 end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜