0

I'm fairly new to rails and struggling on changing database values after the user successfully paid via stripe. Additionally after paying, it somehow redirects me everytime to '/subscriberjobs/1' which doesn't exist. Instead it should direct to the root_path of the application.

Here is what I've got:

Routes

resources :subscriberjobs
resources :jobs

Jobs Controller

def new
  if current_user
    @job = current_user.jobs.build
  else
    redirect_to new_user_session_path
  end
end
def create
  @job = current_user.jobs.build(job_params)

  if @job.save
    redirect_to '/subscriberjobs/new'
  else
    render 'new'
  end
end

Subscriberjobs Controller (Here is what doesn't work!)

class SubscriberjobsController < ApplicationController

    before_filter :authenticate_user!

    def new
    end

    def update

        token = params[stripeToken]

        customer = Stripe::Customer.create(
            card: token,
            plan: 1004,
            email: current_user.email
            )

        Job.is_active = true # doesn't work
        Job.is_featured = false # doesn't work
        Job.stripe_id = customer.id # doesn't work
        Job.save # doesn't work

        redirect_to root_path # doesn't work

    end
end

Please tell me if you need additional information. Every answer is very appreciated. Thanks!

Gugubaight
  • 187
  • 13
  • something errors ? whatever ? – Bartłomiej Gładys Sep 11 '16 at 11:24
  • what are you trying to do at this statement? - Job.is_active. – dnsh Sep 11 '16 at 11:42
  • @Bartek Gładys An error because it's directing me to a page which doesn't exist. That's why I would like to redirect to root_path. Additionally I saw in the console that `is_active` is still false and not true – Gugubaight Sep 11 '16 at 11:42
  • @vyom `is_active` is a param of Job Model. => I've created a job => then I get redirected to the payments and after I've payed it should set the `is_active` param of the Job just created to `true` => and then it should redirect to the root_pah – Gugubaight Sep 11 '16 at 11:43
  • @Gugubaight I think you should access it like object.is_active , why are you calling it like Class.is_active ? – dnsh Sep 11 '16 at 11:45
  • How do I get the object? I somehow have to send it's id... – Gugubaight Sep 11 '16 at 11:46
  • @Gugubaight, Are you able to run that same line on rails console successfully? I am assuming that is_active is property of Job Model's object not of Job Model's. – dnsh Sep 11 '16 at 11:48
  • @Gugubaight, you should create a new object or find existing saved in database and then set is_active to it. – dnsh Sep 11 '16 at 11:50
  • I am able to get the value of it wth: `Job.first.is_active` -> `=> false` So I struggle on getting the id of it. – Gugubaight Sep 11 '16 at 11:51
  • In your post you have written Job.is_active – dnsh Sep 11 '16 at 11:52
  • Yep, but how do send the id from the creation in the Jobs controller to the Subscriberjobs controller? – Gugubaight Sep 11 '16 at 11:53
  • Do you have a `root` defined in your `routes.rb`? Otherwise `root_path` will not work. – spickermann Sep 11 '16 at 12:22
  • @spickermann I do have a root defined: `authenticated :user do root 'tools#index', as: "authenticated_root" end root 'welcome#index'` – Gugubaight Sep 11 '16 at 12:26

1 Answers1

1

Send saved job id to subscriberjobs/new as a param. You can keep hidden field which will have value job_id in subscriberjobs/new html form, which will call your SubscriberjobsController#update method. There access it using params.

In JobController #create

redirect_to "/subscriberjobs/new?job_id=#{@job.id}"

In your SubScribeJob form

hidden_field_tag 'job_id', params[:job_id]

In your SubScribeJobCotroller

@job = Job.find(params[:job_id])
dnsh
  • 3,516
  • 2
  • 22
  • 47
  • Thanks in advance for the explanation! I somehow get this error: `undefined method `merge' for nil:NilClass` for the form line (`= f.hidden_field 'job_id', params[:job_id]`). Never had that. What is merge? – Gugubaight Sep 11 '16 at 12:16
  • @Gugubaight, check this http://stackoverflow.com/questions/6636875/rails-hidden-field-undefined-method-merge-error – dnsh Sep 11 '16 at 12:19
  • Hmm... I get the right url: `http://localhost:3000/subscriberjobs/new?job_id=2`, but after updating this error appears: `ActiveRecord::RecordNotFound in SubscriberjobsController#update Couldn't find Job with 'id'=` and it directs me to this page: `http://localhost:3000/subscriberjobs/1` The value for `is_active` is still `false` – Gugubaight Sep 11 '16 at 12:42