codegirl codegirl - 11 months ago 36
Ruby Question

Accessing data from model in Ruby on rails app

In my app Current user sets his general goal through a form in the

index.html.erb


now I want to bind that value to a instance variable and display this instance variable in the view.

I'm rather new to ruby on rails so I'm not sure how to manage that.

I have tried various types of code to access the data with out any luck.
Can anyone help me with that?

for example:

@general_goal = Goal.find(params[:id])
general_goal = @general_goal.user.general_goal


gives the error:
Couldn't find Goal with 'id'=

and

@general_goal = Goal.find(params[:general_goal])


also gives the error:
Couldn't find Goal with 'id'=


this is the
goals_controller.rb


class GoalsController < ApplicationController

before_action :set_goal, only: [:edit, :update, :show, :destroy]
#before_action :goal_owner, only: [ :edit, :update, :destroy]

def new
@goal = current_user.goals.build
end

def index
@Newgeneralgoal = Goal.new

@goals = current_user.goals.order(created_at: :desc).limit(5)
end

def create
@goal = Goal.new(goal_params)
@goal.user_id = current_user.id

if (@goal.save)
redirect_to transaction_path(current_user), :notice => "Goal has been saved successfully."
else
f.html { redirect_to "", notice: "Error: Goal Not created!!"}
end

end

def update
if @goal.update(goal_params)
flash[:success] = "Goal was successfully updated"
redirect_to goals_path(@goal)
else
render 'edit'
end

def destroy
@goal.destroy
flash[:danger] = "Goal was successfully destroyed"
redirect_to goal_path(current_user)
end

private

def set_goal
@goal = Goal.find(params[:id])
end

def goal_params
params.require(:goal).permit(:user_id, :general_goal, :date)
end
end


I can access the goal in the view with:

<% @goals.each do |goal| %>
<tr>
<td><%= goal.date.try(:strftime,"%Y/%m/%d")%></td>
<td><%= goal.general_goal %></td>
</tr>
<% end %>


so I am basically asking, if the user sets the general goal to maybe "5" how can I grab that "5" from the database and assign it to a variable?

here is the
schema.rb
for goals

create_table "goals", force: :cascade do |t|
t.datetime "date"
t.decimal "general_goal"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end


and here is the goal.rb model

class Goal < ActiveRecord::Base
belongs_to :user
end


here is the user.rb model

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

has_many :goals

end

Answer Source

If I understand you correctly, You want to show the value of :general_goal for current_user?

This might do the trick for you, just put this code in the indexin the controller. I haven't tested it, but it might work.

current_general_goal = @current_user.goals.sum(:general_goal)     
@general_goal =  current_general_goal.nil? ? 0 : current_general_goal

It checks if the :general_goal is nil, if so it displays 0, if not it displays the general goal value.

update

If you want the value from the last general_goalfrom the current user. You can add this code to your index view.

current_general_goal = @current_user.goals.last.general_goal
@general_goal =  (current_general_goal.nil? ? 0 : current_general_goal) 

Please let me know if this works for you

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download