I've been following a tutorial Follower Tutorial and I keep getting the error undefined method `find_by_username' for the line.
<%= render '/components/follow_button', :user => User.find_by_username(params[:id]) %>
show.html.erb
<%= render '/components/follow_button', :user => User.find_by_username(params[:id]) %>
<div class="panel panel-default">
<div class="panel-body">
<h5 style="color: grey; font-size: 125%;">Who to follow</h5>
<% for #u in #toFollow do %>
<p style="font-weight: bold; opacity: 0.85;" ><%= #u.username %></p>
<% end %>
</div>
</div>
user.rb
def unfollow(other)
active_relationships.find_by(followed_id: other.id).destroy
_follow_button.html.erb
<% if current_user.id != user.id %>
<div class="panel panel-default">
<div class="panel-body">
<center>
<% if !current_user.following?(user) %>
<%= form_for(current_user.active_relationships.build) do |f| %>
<div><%= hidden_field_tag :followed_id, user.id %></div>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
<% else %>
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id),
html: { method: :delete }) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
<% end %>
</center>
</div>
</div>
I've been messing around for ages now and can't work it out. Any help would be appreciated. Feel free to ask for more code etc.
In newer version of rails you should use following code:
Inside controller:
#user = User.find_by(username: username)
In Rails 4 onwards the find_by_... methods are deprecated therefore your User.find_by_username will not work for Rails 4 and higher. You should be able to use either:
User.find(id)
or
User.where(:username=> username)
Related
I've got a form to add SaleQualifiers to my app - which works fine when I'm using:
<%= form_for(#sale_qualifier, :html => {role: :form, 'data-model' => 'sale_qualifier'}, remote: true) do |f| %>
The problem with form_for is that I want to put the form inline within a table row in my view - so as a method to get around that I'm now using:
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
This is working fine for most of the fields I need to generate, but I've got a nested attribute field for Answer (Answer belongs_to SaleQualifier). This is not generating the right field names in the view, and as a result when I go to save the object this way I don't capture the answer_attributes.
Here's the full working form using form_for:
<div class="panel panel-default">
<%= form_for(#sale_qualifier, :html => {role: :form, 'data-model' => 'sale_qualifier'}, remote: true) do |f| %>
<% if #sale_qualifier.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#sale_qualifier.errors.count, "error") %> prohibited this answer from being saved:</h2>
<ul>
<% #sale_qualifier.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="panel-body">
<div class="col-sm-6">
<h2><%= #question.question_text %></h2>
<% unless #question.id == 1 %>
<p><%= link_to('Back', edit_sale_qualifier_path(id: #prior_sale_qualifier), data: { disable_with: "Loading..." }, :remote => true) %></p>
<% end %>
</div>
<div class="col-sm-6">
<div class="form-group">
<%= f.hidden_field :sales_opportunity_id, :value => #sales_opportunity.id %>
</div>
<div class="form-group">
<%= f.hidden_field :question_id, :value => #question.id %>
</div>
<% unless #question.id == 1 %>
<div class="form-group">
<%= f.hidden_field :prior_question_id, :value => #prior_question_id %>
</div>
<% end %>
<%= f.fields_for :answer do |answer| %>
<div class="form-group">
<% if #question.answer_type == 'Text Field' %>
<%= answer.text_area :answer_text, :placeholder => "Enter your answer", :class => "form-control"%>
<% end %>
<% if #question.answer_type == 'Datetime' %>
<div class='input-group date' id='datetimepicker' data-date-format="YY.MM.DD">
<%= answer.text_field :answer_text, class: "form-control", data: { date_format: 'YYYY/MM/DD' }, :placeholder => "YYYY/MM/DD" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<% end %>
<% if #question.answer_type == 'Boolean' %>
<%= answer.select :answer_text, [['Yes', true], ['No', false]] %>
<% end %>
<% if #question.answer_type == 'Update' || #question.answer_type == 'Result' %>
<%= answer.hidden_field :answer_text, :value => "Updated" %>
<% end %>
<span class="warning-block"></span>
<span class="help-block"></span>
</div>
<% end %>
<% if #question.answer_type == 'Update' || #question.answer_type == 'Result' %>
<div class="actions">
<%= f.submit "Done", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</div>
<% else %>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
Here's the code which does not work using form_tag:
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
<%= ff.hidden_field :sales_opportunity_id, :value => #sales_opportunity.id %>
<%= ff.hidden_field :question_id, :value => #question.id %>
<tr>
<td><%= #question.question_text %></td>
<td>
<%= ff.fields_for :answer do |answer| %>
<% if #question.answer_type == 'Text Field' %>
<%= answer.text_area :answer_text%>
<% end %>
<% if #question.answer_type == 'Datetime' %>
<div class='input-group date' id='datetimepicker' data-date-format="YY.MM.DD">
<%= answer.text_field :answer_text, class: "form-control", data: { date_format: 'YYYY/MM/DD' }, :placeholder => "YYYY/MM/DD" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<% end %>
<% if #question.answer_type == 'Boolean' %>
<%= answer.select :answer_text, [['Yes', true], ['No', false]] %>
<% end %>
<% end %>
</td>
<td>
<%= ff.submit "Submit", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</td>
</tr>
<% end %>
<% end %>
For completeness, the issue I'm having is that the generated html for the working code creates the following field:
textarea id="sale_qualifier_answer_attributes_answer_text" name="sale_qualifier[answer_attributes][answer_text]"
The broken code creates the following html:
Textarea id="sale_qualifier_answer_answer_text" name="sale_qualifier[answer][answer_text]"
So how can I get the html output to show "sale_qualifier[answer_attributes][answer_text]" rather than "sale_qualifier[answer][answer_text]" in this instance using form_tag?
In multiple nested forms the fields_for tag will be called against
right parent and attributes conventions should be followed right otherwise
it renders the form attributes wrong and results in errors.
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
<%= ff.fields_for :answer_attributes do |answer| %>
Above flow will be the right one & will generate attributes as they should be.
I have a form called cattle finances, in this form, i use collection select to input the batch number, i have other form columns shown in the _form.html.erb code below
<%= simple_form_for #cattle_finance, remote: true do |f| %>
<% if #cattle_finance.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#cattle_finance.errors.count, "error") %> prohibited this cattle_finance from being saved:</h2>
<ul>
<% #cattle_finance.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<br>
<%= f.collection_select :batch_number, CattleList.all, :batch_number, :batch_number, :prompt => "please select Batch Number" %></br>
<%= f.input :total_litres_of_batch, as: :integer %>
<%= f.input :total_income, as: :integer %>
<%= f.input :gross_income, as: :integer %>
<%= f.input :total_expenses, as: :integer %>
<%= f.input :net_income, as: :integer %>
<%= f.input :date, as: :date %>
<%= f.button :submit %>
<% end %>
My index.html.erb code is
<div class="row">
<div class="col-md-5 col-md-offset-1">
<h2>Cattle Finances</h2>
</div>
<div class"btn-group menu2" align = right>
<%= link_to raw("<span class=''></span> Creditors"), cattle_creditors_path, :class=>"btn btn-default" %>
<%= link_to raw("<span class=''></span> Debtors"), cattle_debtors_path, :class=>"btn btn-default" %>
<%= link_to raw("<span class=''></span> Cattle"), cattles_path, :class=>"btn btn-default" %>
<%= link_to "Home", root_path, :class=>"btn btn-default" unless current_page?(root_url) %>
</div>
<div class="col-md-2 col-md-offset-4">
<%= link_to new_cattle_finance_path, remote: true do %>
<button class="btn btn-default">New</button>
<% end %>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-2" id="cattle_finance-form" style="display:none;"></div>
</div>
<div class="row">
<div class="" id="cattle_finances"><%= render #cattle_finances %></div>
</div>
So what is need help with is the way in which i can select a particular batch_number and automatically all other fields are populated, or picked from the database from the corresponding database tables and filled in respectively. Any help his highly appreciated coz am stuck, although
i know what i want to do, I have no idea of how i can start.
A beginner in Ruby here!
I am trying to create a Soundcloud clone on ruby.
When I try to upload an audio file i get the error:
1 error prohibited this song from being saved:
Audio has contents that are not what they are reported to be
controller: song.rb
class Song < ActiveRecord::Base
belongs_to :user
has_attached_file :audio,
:url => "/assets/:class/:id/:attachment/:basename.:extension",
:path => ":rails_root/public/assets/:class/:id/:attachment/:basename.:extension"
validates_attachment :audio,
:content_type => { :content_type => ["audio/mpeg", "audio/mp3"] },
:file_name => { :matches => [/mp3\Z/] }
_form.html.erb
<%= form_for(#song) do |f| %>
<% if #song.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#song.errors.count, "error") %> prohibited this song from being saved:</h2>
<ul>
<% #song.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :audio %><br>
<%= f.file_field :audio%>
</div>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Audio:</strong>
<%= #song.audio.url %>
</p>
<p>
<strong>Title:</strong>
<%= #song.title %>
</p>
<%= link_to 'Edit', edit_song_path(#song) %> |
<%= link_to 'Back', songs_path %>
In my experience with this problem, image data in the mp3's metadata (i.e. - an album cover) was the critical factor. The image data triggers the content type spoofing mechanism and the record will fail validation. To remove the image metadata, I opened the mp3 with Audacity, and exported it to a new file, which will not include image metadata because Audacity doesn't seem to include this automatically.
I am creating a job portal application in Rails 4.1 using Ruby 1.9
The application initially was running fine, but unexpectedly an error popped up. The jobs subdirectory in the views directory contains two files new.html.erb and _form.html.erb(this is a partial file). While rendering the new file, the partial file is also rendered from the new file.
But, the application is showing the following error in Rubymine
ERROR DETAILS:
"NoMethodError in Jobs#new"
Showing E:/RailsProject/Old/FinalSubm/jobportal/app/views/jobs/_form.html.erb where line #23 raised:
undefined method `error_span' at line 23
20 <div class="controls">
21 <%= f.text_field :title, :class => 'form-control' %>
22 </div>
23 <%= error_span(#job[:title]) %>
24 </div>
25 <div class="control-group">
26 <%= f.label :description, :class => 'control-label' %>
Trace of template inclusion: app/views/jobs/new.html.erb
Rails.root: E:/RailsProject/Old/FinalSubm/jobportal
Application Trace | Framework Trace | Full Trace
app/views/jobs/_form.html.erb:23:in block in _app_views_jobs__form_html_erb__604570268_42022236'
app/views/jobs/_form.html.erb:1:in_app_views_jobs__form_html_erb__604570268_42022236' app/views/jobs/new.html.erb:5:in `_app_views_jobs_new_html_erb___144564004_42034104'
The following is the code written for E:/RailsProject/Old/FinalSubm/jobportal/app/views/jobs/_form.html.erb
<%= form_for #job, :html => { :class => "form-horizontal job" } do |f| %>
<% if #job.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(#job.errors.count, "error") %> prohibited this job from being saved:</h3>
</div>
<div class="panel-body">
<ul>
<% #job.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<div class="control-group">
<%= f.label :title, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :title, :class => 'form-control' %>
</div>
<%= error_span(#job[:title]) %>
</div>
<div class="control-group">
<%= f.label :description, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :description, :class => 'form-control' %>
</div>
<%= error_span(#job[:description]) %>
</div>
<div class="control-group">
<%= f.label :tag1, :class => 'control-label' %>
<div class="controls">
<%= f.select :tag1, options_for_select(%w[Internship Full-Time Part-Time Co-Op Contractor]) %>
</div>
<%= error_span(#job[:tag1]) %>
</div>
<div class="control-group">
<%= f.label :tag2, :class => 'control-label' %>
<div class="controls">
<%= f.select :tag2, options_for_select(%w[Work-From-Home Office]) %>
</div>
<%= error_span(#job[:tag2]) %>
</div>
<div class="control-group">
<%= f.label :tag3, :class => 'control-label' %>
<div class="controls">
<%= f.select :tag3, options_for_select(%w[US-Citizen Non-US-Citizen]) %>
</div>
<%= error_span(#job[:tag3]) %>
</div>
<div class="control-group">
<%= f.label :category, :class => 'control-label' %>
<div class="controls">
<%= f.select :category_name, Category.all.collect{ |c| [c.name, c.id]} %>
</div>
<%= error_span(#job[:category_name]) %>
</div>
<div class="control-group">
<%= f.label :deadline, :class => 'control-label' %>
<div class="controls">
<%= f.date_select :deadline, :class => 'form-control' %>
</div>
<%= error_span(#job[:deadline]) %>
</div>
<!-- <div class="control-group">
<%= f.label :employer_id, :class => 'control-label' %>
<div class="controls">
<%= f.number_field :employer_id, :class => 'form-control' %>
</div>
<%= error_span(#job[:employer_id]) %>
</div>
<%= f.hidden_field :employer_id %>
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
jobs_path, :class => 'btn btn-default' %>
<% end %>
The following is the code written for E:/RailsProject/Old/FinalSubm/jobportal/app/views/jobs/new.html.erb
<%- model_class = Job -%>
<div class="page-header">
<h1><%=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize %></h1>
</div>
<%= render :partial => 'form' %>
The program was running fine a couple of days back, no changes have been made to the code since then, apart from restarting the rails server. I am trying to figure out where this error popped from. To solve this error should I define the method error_span in Jobs#new controller ?. What should be written in it ? OR Should I remove the error_span statement from the views, but this would imply that many changes would have to be made in the code, since I have 8 to 10 more files where the partial file mentioning the html statement needs to be changed.
If you're using twitter-bootstrap-rails (which is where I'm guessing the error_span method comes from).
Try using the latest code by putting in your Gemfile:
gem 'twitter-bootstrap-rails', :git => 'https://github.com/seyhunak/twitter-bootstrap-rails.git', :branch => 'master'
Because it looks like they fixed this method a few weeks ago.
more info:https://github.com/seyhunak/twitter-bootstrap-rails/pull/795
twitter-bootstrap-railshas a bug in lib/generators/bootstrap/themed/templates/
<%%= error_span(#<%= resource_name %>[:<%= column.name %>]) %>
change to
<%%= f.error_span(:<%= column.name %>) %>
.erb && slim templete has the same bug.
here is a fixed version
Based on #NoelProf's answer, I changed my Gemfile:
gem 'twitter-bootstrap-rails', '>= 3.2.0', '< 3.2.2'
This will not install the version 3.2.2.
I restarted the server and the error_span problem has been gone.
I have a form that works fine until I add the tags for styling (I am using twitter bootstrap). The app is Rails 3.1 and I am on a mac. Here is a form that works fine with no issues:
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="alert-message error">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit %></div>
<%end %>
Then, when I add the tag to the same exact form like this:
<form class="form-stacked">
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="alert-message error">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit %></div>
<%end %>
</form>
This form appears to issue a GET request instead of a POST request. Instead of redirecting me upon save like it does without the tag, I see this in the URL:
http://localhost:3000/signup?utf8=%E2%9C%93&authenticity_token=Pt1vOp7lykCPFdj5BJeZ6xwJM2vy0JomMGSKoB%2FyYpU%3D&user%5Bemail%5D=test%40get.com&user%5Bpassword%5D=kevin&user%5Bpassword_confirmation%5D=kevin&commit=Create+User
I have tried specifying post in both the rails and HTML. Specifying it in rails using :method => post yields the same results (the GET request and URL info above). Using in the HTML looks like a POST request but the User object isn't saving (I belive this is the case because I am not getting redirected to the root_url, which is set in the controller and works fine when not using tag). Any ideas?
you want this:
<%= form_for #user, :html => { :class => "form-stacked", :id => "something" } do |f| %>
stuff goes in here as before
<% end %>