Error in foreign key association in Rails - ruby-on-rails

I have two models:
user.rb
class User < ActiveRecord::Base
...
has_many :reports, :foreign_key => "assigned_user_id"
...
end
report.rb
class Report < ActiveRecord::Base
belongs_to :assigned_user, :class_name => "User"
end
index action
def index
#all_reports = Report.all
end
index.html.erb
<% #all_reports.each do |report| %>
<%= report.user.email %> </br>
<% end %>
This throws me the error:
undefined method `user' for #<Report:0x007f90dc642e80>
What am I missing? My other - standard rails - associations work perfectly.

Change this line:
<%= report.user.email %> </br>
To:
<%= report.assigned_user.email %> </br>

Related

Ruby on Rails: Polymorphic Association with Nested Attributes, fields not showing

I have a polymorphic association form and I'd like to build a nested form, but the fields are not showing up:
views/reviews/_form.html.erb:
<%= form_for [#reviewable, #review] do |f| %>
<%= f.fields_for :review_images do |i| %>
<%= i.file_field :image %>
<% end %>
<% end %>
review.rb:
class Review < ActiveRecord::Base
attr_accessible :review_styles_attributes
belongs_to :reviewable, polymorphic: true
has_many :review_styles
accepts_nested_attributes_for :review_images, allow_destroy: true
end
review_image.rb:
class ReviewStyle < ActiveRecord::Base
attr_accessible :review_id, :image
belongs_to :reviewable, polymorphic: true
belongs_to :review
end
reviews_controller.rb:
class ReviewsController < ApplicationController
before_filter :get_reviewable
def new
#review = #reviewable.reviews.new
#review_style = #review.build_review_style
3.times {#review.review_styles.new}
end
def edit
# not sure what goes here if I need to edit as well
end
private
def get_reviewable
#reviewable = params[:reviewable].classify.constantize.find(reviewable_id)
end
def reviewable_id
params[(params[:reviewable].singularize + "_id").to_sym]
end
end
I think your problem is here:
<%= f.fields_for :review_images do |i| %>
<%= i.file_field :image %>
<% end %>
From looking at your code, it should be:
#app/views/reviews/new.html.erb
<%= f.fields_for :review_styles do |i| %>
<%= i.file_field :image %>
<% end %>
#app/controllers/reviews_controller.rb
def new
#review = #reviewable.reviews.new
#review.review_styles.build
end
You should note when you're building associative values, you should use .build for plural / multiple associations, and build_ for singular

param not found: generator

im trying to build my 3rd model(result) along with 2nd model ( generator). I call the result form from my Generator show.html.erb and when i clicked the submit button in the form i get this error param not found: generator
routes.rb
resources :users do
resources :generators
resources :results
end
Generator show.html.erb
...
<legend><strong>Binding Time Analysis</strong></legend>
<%= render "results/form" %>
</fieldset>
<% end %>
Result.controller
def new
#result=Result.new
end
# GET /results/1/edit
# POST /results
# POST /results.json TGATGAACATCATGATGAGGTGATGACATCACATCATTGACTGATGCATCATGATG
def create
#result = #generator.build_result(result_params)
#result=#result.generate_result(result_params)
#generator.result.save
redirect_to user_generators_path
end
def result_params
params.require(:result).permit(:ncbi_ref_seq,:genome_seq,:genome_sample,:binding_times,:amp_frags,:seqpos1,:seqpos2)
end
User.rb
class User < ActiveRecord::Base
has_many :generators
has_many :results, :through=>:generators
Generator.rb
class Generator < ActiveRecord::Base
has_one :result , :dependent => :destroy
belongs_to :user
attr_accessible :choice, :primer_length, :random_primer_generated, :generator_id
Result.rb
class Result < ActiveRecord::Base
attr_accessible :generator_id,:ncbi_ref_seq,:genome_seq, :genome_sample
belongs_to :generator
Form
<%= form_for(#generator.build_result,:url =>user_generator_path(:user_id => current_user.id, :id => #generator.id),:html =>{:method=>:put}) do |f| %>
<%= text_field_tag(:ncbi_ref_seq ,nil, placeholder: 'Accession Number')%>
</p>
<p id="highlight"><font size ="5">
OR
</font></p>
<p id="FASTA">
<strong><font size ="3">Paste your sequence here: ( FASTA format ) :</font></strong>
<%= text_area_tag(:genome_seq,nil,size: "50x10",placeholder: 'Input your sequence here')%>
</p>
<input type="reset" value="Reset" id="reset">
<%= submit_tag 'Analyze' %>
<% end %>
Generator.controller generator_params
def generator_params
params.require(:generator).permit(:generator_id,:primer_length,:choice,:random_primer_generated,:no_A,:no_T,:no_G,:no_C,:user_seq)
end

Rails: Polymorphic Associations: How to list associations

I have a polymorphic assocation:
class User < ActiveRecord::Base
belongs_to :companiable, :polymorphic => true
end
class Agency < ActiveRecord::Base
has_many :users, :as => :companiable
end
class Publisher < ActiveRecord::Base
has_many :users, :as => :companiable
end
and now I want to list all users and show the company they belong to. Is there a more elegant solution than this (strongly hope there is)?
def index
#publishers = Publisher.all
#agencies = Agency.all
#users = User.all
end
...
<td><% unless user.companiable_id.nil? %>
<% if user.companiable_type == "Agency" %>
<%= #agencies[user.companiable_id].name %>
<% elsif user.companiable_type == "Publisher"%>
<%= #publishers[user.companiable_id].name %>
<% end %>
<% end %>
</td>
You can acces the company from user, since the belongs_to adds a method so that you can access the other object directly by doing user.companiable
def index
#users = User.all
end
and in your view
<td><% unless user.companiable.nil? %>
<%= user.companiable.name %>
<% end %></td>

I get the warning "Can't mass-assign protected attributes"

When i try to submit the form below i get this error WARNING: Can't mass-assign protected attributes: sub_category.I have tried to go over previous asked related questions here on stackoverflow and seems like i am in the right track ,but for some reason i am still getting the same error,what am i doing wrong?.I have included all the info below, thank you in advance.
View/form
<%= form_for #ip ,:url=>{:action =>"create"} do |f| %>
<%=f.text_field :email %>
<% f.text_field :ip_address %>
<%= f.fields_for :sub_category do |s| %>
<%=s.text_field :name%>
<%end%>
<%=f.submit "submit" %>
<%end%>
Controller
def create
#ips=Ip.new(params[:ip])
#ip=#ips.sub_categories.build
if #ip.save
redirect_to :controller=>"home" ,:action=>"index"
else
render 'index'
end
Models
class Ip < ActiveRecord::Base
has_many :sub_categories ,:through=>:ip_subs
has_many :ip_subs
accepts_nested_attributes_for :sub_categories
attr_accessible :sub_categories_attributes,:ip_address,:email,:ip_count
end
class SubCategory < ActiveRecord::Base
has_many :ip ,:through=>:ip_subs
has_many :ip_subs
end
class IpSub < ActiveRecord::Base
belongs_to :ip
belongs_to :sub_category
end
You should use f.fields_for :sub_categories (association name).
And don't forget to build association before render the form:
# in controller
def new
#ip = Ip.new
#ip.sub_categories.build
end
rubyonrails api :: fields_for

Rails: Create form for #score while in different model no direct associations

I want to create a multiple form for editing scores from a different model.
The main model is a Formrule model that consists of a habtm association with a Scoretype model
and has a habtm association with a Room model.
Both models are used to query a Scores model resulting in a #scores instance. It is for this instance I want to create a form, but the problem is that no field_for are being created. I know that the #scores is populated correctly, but the form does not show up.
This is the form as I have it now
<%= form_tag '/scores/update_scores' do %>
<table>
<tr>...</tr>
<% for score in #scores %>
<% fields_for :scores, score do |score| %>
<tr>
<td>
<%= score.hidden_field(:form_id) %>
<%= score.hidden_field(:team_id) %>
<%= score.hidden_field(:scoretype_id) %>
</td>
<td>
<%= score.number_field :scorevalue %>
</td>
</tr>
<% end %>
<% end %>
</table>
<%= submit_tag 'Update' %>
<% end %>
And these are the Models:
Formrule
class Formrule < ActiveRecord::Base
belongs_to :form
has_and_belongs_to_many :scoretypes
has_and_belongs_to_many :rooms
has_many :teams, :through => :rooms
end
Scoretype
class Scoretype < ActiveRecord::Base
has_many :scores
has_and_belongs_to_many :formrules
end
Room
class Room < ActiveRecord::Base
has_many :teams
has_and_belongs_to_many :formrules
end
Team
class Team < ActiveRecord::Base
has_many :scores
belongs_to :room
belongs_to :group
end
Score
class Score < ActiveRecord::Base
belongs_to :form
belongs_to :team
belongs_to :scoretype
validates_uniqueness_of :id, :scope => [:team, :scoretype]
end
And finally, the used controller (Formrule)
def show
#formrule = Formrule.find(params[:id])
#scoretypes = #formrule.scoretypes.all.collect
#rooms = #formrule.rooms.all.collect
#teams = Team.find(:all, :conditions => {:room_id => #rooms})
#scores = Score.order("team_id").all(:conditions => {:scoretype_id => #scoretypes, :team_id => #teams})
...
end
Why is the form not showing up? any suggestions?
Thank you all in advance!
Try using <%= fields_for ... %> instead of <% fields_for ...%>.

Resources