Rails select query displaying all columns with odd formatting in view -
i'm new rails , i've nail first wall. until i've been able utilize google answer. honest, think part of problem i'm unable explain problem.
my rails select query in show.html.erb file (1) displaying duplicate records , (2) displaying info odd formatting instead of plain text. i'll paste code below illustrate.
i have user model has_many :works
class user < activerecord::base # include default devise modules. others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # setup accessible (or protected) attributes model attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :works_attributes # attr_accessible :title, :body validates_presence_of :first_name, :last_name validates_uniqueness_of :first_name, :last_name, :email, :case_sensitive => false has_many :works accepts_nested_attributes_for :works
here work model, belongs_to user
class work < activerecord::base attr_accessible :description, :work_name, :users_id belongs_to :user end
and here users controller:
class userscontroller < applicationcontroller def index @users = user.all respond_to |format| format.html # index.html.erb format.xml { render :xml => @users } end end def show @user = user.find(params[:id]) @works = @user.works.all respond_to |format| format.html # show.html.erb format.xml { render :xml => @user } end end end
in users show.html.erb view, want loop through works belong particular user. setup foreign key relationship user_id field follow rails convention. when loop finds work belongs user, want display work_name field.
<% if @user.works.any? %> <% @user.works.each |user| %> <%= @user.works(:select => 'work_name') %> <% end %> <% end %>
however, output i'm seeing on website when viewing /users/1/:
[#<work id: 1, work_name: "worktest1", description: "i co-founder , ceo of worktest1, website t...", created_at: "2013-02-12 00:33:19", updated_at: "2013-02-12 00:33:19", user_id: 1>, #<work id: 2, work_name: "worktest2", description: "test", created_at: "2013-02-12 00:33:19", updated_at: "2013-02-12 00:33:19", user_id: 1>] [#<work id: 1, work_name: "worktest1", description: "i co-founder , ceo of worktest1, website t...", created_at: "2013-02-12 00:33:19", updated_at: "2013-02-12 00:33:19", user_id: 1>, #<work id: 2, work_name: "worktest2", description: "test", created_at: "2013-02-12 00:33:19", updated_at: "2013-02-12 00:33:19", user_id: 1>]
your .each loop not looping on users work objects.
you should able do:
<% @user.works.each |work| %> <%= work.work_name %> <% end %>
also, since defining @work in controller action can cut down to:
<% @works.each |work| %> <%= work.work_name %> <% end %>
you have work objects @ disposal. don't think there reason seek select else.
ruby-on-rails ruby-on-rails-3.2
No comments:
Post a Comment