show_for gem gives to_key error - ruby-on-rails-3

hi i am using show_for gem and it gives me error like: undefined method `to_key' for # in app/views/messages/index.html.erb where line #46 raised:
my view look like these:
46: <%= show_for #messages do |s| %>
47: <%= s.attribute :subject %>
48: <%= s.attribute :message %>
49: <% end %>
my controller code look like:
#messages= Message.all
please help me what i am doing wrong. thanks in advance

I do not know this gem but according to its documentation, show_for is not supposed to receive a collection as argument (#messages) but a single object. I might be wrong.
With this supposition, I would to :
<% #messages.each do |message| %>
<%= show_for message do |s| %>
<%= s.attribute :subject %>
<%= s.attribute :message %>
<% end %>
<% end %>

Related

undefined method `model_name' for NilClass:Class in edit form

I have a rails app where I can create and edit records. I've created a form to enter data which works fine when I use the new/create actions. It will create a record no problem. But when I hit the edit action it gives me an undefined method 'model_name' for NilClass:Class.
I'm not sure what this means. Can someone give me a hand?
Form:
<%= form_for(#patient) do |f| %>
<%= f.label :Patient_Last_Name %>
<%= f.text_field :patient_last %>
<%= f.label :Patient_First_Name %>
<%= f.text_field :patient_first %>
<%= f.label :Patient_DOB %>
<%= f.date_select :patient_dob %>
<%= f.label :Primary_Diagnosis %>
<%= f.collection_select(:diagnosis_id, Diagnosis.all, :id, :diagnosis_name)%></br>
<%= f.label :Primary_Physician %>
<%= f.collection_select(:physician_id, Physician.all, :id, :physician_name)%></br>
<%= f.button :submit %>
<% end %>
View Code:
<td><%= link_to 'Edit', edit_patient_path(patient), :class => 'btn btn-close btn-mini'%></td>
Controller Code:
def edit
#patient = Patient.find(params[:id])
end
Edit view:
<%= render 'form' %>
When I remove the partial render from the form, the URL will go to the correct route/url. But I keep getting that error when the form partial is rendered.
There was an extra end statement in my controller which was cutting off half of the class which included the edit action. This was not allowing me to use the edit action. Once this typo was fixed things started working normally.
Sorry for the confusion.

Weird undefined method 'all' and collection_select error

I've got a form like this (simplified, but you get the idea):
<%= form_for(#brand, :html => { :class => "form-horizontal" }) do |f| %>
<%= f.fields_for :prices do |price| %>
<%= price.collection_select(:price, :template_id, Template.all, :id, :name) %>
<% end %>
<%= f.submit "Save", :class => 'btn btn-primary' %>
<% end %>
Which when rendered gives me this error
undefined method `all' for ActionView::Template:Class
on the collection_select line.
Template.all works from the controller and the console. If I write a #templates = Template.all and use #templates in the collection_select line then I get this error:
undefined method `merge' for :name:Symbol
Any thoughts?
You can do it by prefixing with two colon. e.g,
<%= price.collection_select(:price, :template_id, ::Template.all, :id, :name) %>
but I believe, you should avoid using Template as model name as it is rails Action View Template
Solved it. It was annoyingly simple.
<%= price.collection_select(:template_id, #templates, :id, :name) %>
Duplication. Eugh.

How can I fix this error: "incompatible character encodings: UTF-8 and ASCII-8BIT"?

I am trying to use the rmmseg-cpp gem's sample code documented here: http://rmmseg-cpp.rubyforge.org/#Stand-Alone-rmmseg
Just to test it out I put it in show.html.erb like this:
# coding: UTF-8
<p id="notice"><%= notice %></p>
<p>
<b>Title:</b>
<%= #lesson.title %>
</p>
<p>
<b>Content:</b>
<%= #lesson.content %> # simplified chinese text
</p>
<p><% require 'rmmseg' %>
<% algor = RMMSeg::Algorithm.new(#lesson.content) %>
<% loop do %>
<% tok = algor.next_token %>
<% break if tok.nil? %>
<%= "#{tok.text} [#{tok.start}..#{tok.end}]" %>
<% end %> </p>
<%= link_to 'Edit', edit_lesson_path(#lesson) %> |
<%= link_to 'Back', lessons_path %>
I get the following error:
Encoding::CompatibilityError in Lessons#show
Showing /Users/webmagnets/rails_projects/blt/app/views/lessons/show.html.erb where line #19 raised:
incompatible character encodings: UTF-8 and ASCII-8BIT
Extracted source (around line #19):
16: <% loop do %>
17: <% tok = algor.next_token %>
18: <% break if tok.nil? %>
19: <%= "#{tok.text} [#{tok.start}..#{tok.end}]" %>
20: <% end %> </p>
21:
22: <%= link_to 'Edit', edit_lesson_path(#lesson) %> |
Rails.root: /Users/webmagnets/rails_projects/blt
Application Trace | Framework Trace | Full Trace
app/views/lessons/show.html.erb:19:in `block in _app_views_lessons_show_html_erb___3831310028264182552_70339844987120'
app/views/lessons/show.html.erb:16:in `loop'
app/views/lessons/show.html.erb:16:in `_app_views_lessons_show_html_erb___3831310028264182552_70339844987120'
app/controllers/lessons_controller.rb:20:in `show'
Request
Parameters:
{"id"=>"1"}
Show session dump
Show env dump
Response
Headers:
None
If you need any more info, please let me know.
This link helped me: https://github.com/sinatra/sinatra/issues/559#issuecomment-7748296
I used <% text = tok.text.force_encoding 'UTF-8' %> and it worked.
Thanks #zed_0xff for putting me on the right path.
try this workaround
<% text = tok.text.encode('utf-8',:invalid => :replace, :undef => :replace) %>
<%= "#{text} [#{tok.start}..#{tok.end}]" %>

Checkbox symbols wrong number of arguments

I am having a problem with the tutorial of codelearn
See here
I have a form
<%= form_for :complete, :url => "/todos/complete", :method => :post do |f| %>
<% #todo_items.each do |t| %>
<%= f.check_box :todo_ids[], t.id %>
<%= t.todo_item %>
<% end %>
<%= f.submit "Complete todos", :class => "btn btn-success" %>
<% end %>`
I've got a problem with the symbol todo_ids[]. I get the error "wrong number of arguments (0 for 1..2)" at the line where it is written.
I tried another way with form_tag but that does not change a thing, I still get the error.
What I don't understand is that they don't have this problem in the tutorial.
Please do you have any idea ?
Many thanks

How do you pass rails 3 forms params into an array?

I have a nested form (using accepts_nested_attributes_for in respective models):
<%= form_for(:technician, :url => {:controller => 'pos', :action => 'create_ticket'}) do |f| %>
<%= f.fields_for :service do |s| %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<%= s.text_field :name %>
<% end %>
<% end %>
This works fine if I only have one s.text_field. But once I add additional text_fields, it doesn't run properly. If you look at the source code, the id and name are the same for all six?
How do I put these params into an array? [so that I can isolate them like this:]
service1 = Service.named(params[:technician][:service][1][:name])
(I've tried the method described on railscasts episode 192, but it didn't work either).
After hours of trial and error, I've hacked something that works (but please let me know if you know a better, more eloquent way):
in view:
<%= form_for(:technician, :url => {:controller => 'pos', :action => 'create_ticket'}) do |f| %>
<% for #i in 1..6 do %>
<%= f.fields_for "services[#{#i}]" do |s| %>
<% end %>
<% end %>
<% end %>
in controller:
for i in 1..6 do
#service = Service.named(params[:technician][:services][i.to_s][:name]).first
end
make sure you turn i into string