I really can't figure out why I can't access the user_id field for the class Post. I have it as a field in my database (I can see it, and it isn't blank. Yet for some reason, I'm getting this error:
undefined method `user_id' for #<Class:0x103497448>
Extracted source (around line #10):
7: <h2>Topics</h2>
8: <% #board.topics.each do |topic| %>
9: <% #post = topic.posts(1) %>
10: <b><%= User.find(#post.user_id).username %> says <%= link_to topic.subject, [#board, topic] %></b><br />
11: <% end %>
12: <% end %>
13:
topic.posts(1) will return an array. Hence #<Class:0x103497448>
The right way to get the first post for a topic would be
9: <% #post = topic.posts.first %>
10: <b><%= User.find(#post.user_id).username %> says <%= link_to topic.subject, [#board, topic] %></b><br />
Try <% #post = topic.posts.limit(1) %>
Related
I am rendering a variable from the controller action . The code is :
def show1
if request.post?
#bookmark = Bookmark.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #bookmark }
end
end
end
This is the action 'show'. The 'show' view is as follows:
<% #array_bookmark = #bookmark.class == Array ? #bookmark : [#bookmark] %>
<% #array_bookmark.each do |book| %>
<li><%= book.url%>
<b>Title:</b>
<li><%= book.title%>
<% end %>
This should print the records in bookmarks table with attributes url and title. But it gives an error:
NoMethodError in Bookmark#show
Showing C:/Sites/newapp/app/views/bookmark/show.html.erb where line #10 raised:
undefined method `url' for nil:NilClass
Extracted source (around line #10):
7: <b>Url:</b>
8: <% #array_bookmark = #bookmark.class == Array ? #bookmark : [#bookmark] %>
9: <% #array_bookmark.each do |book| %>
10: <li><%= book.url%></li>
11: <b>Title:</b>
12: <li><%= book.title%></li>
13: <li><%= book.tags%></li>
This is the error i get . Can i anyone gimme a proper solution so that i can print the table records in the view properly?
The problem is that you should have used my answer to your earlier question:
<% Array(#bookmark).each do |book| %>
The code you have checks whether #bookmark is an instance of the class Array; if it is nil, this will return false and #array_bookmark will be assigned [nil]. Then when you iterate over it in your each loop you'll get a nil value for book. By using Array() you avoid this problem: Array(nil) evaluates to an empty array, so the loop never executes and you don't get an error.
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}]" %>
Im getting the following error message:
undefined method `user_registration_path' for #<#<Class:0x10f227cd8>:0x10f5f6d50>
Extracted source (around line #3):
1: <h2>Sign up</h2>
2:
3: <%= form_for(resource_name, resource, :url => registration_path(resource_name)) do |f| %>
4: <%= f.error_messages %>
5: <p><%= f.label :email %></p>
6: <p><%= f.text_field :email %></p>
I checked everything and i didnt found an error, i had just created the project and installed the plugin.
I was using a old version of the devise plugin in the new version of the ror. =x
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 %>
UPDATE: THE CODE IN THIS POST HAS BEEN CHANGED TO THE WORKING SOLUTION.
To make radio buttons display stored values from a mapped array.
Controller:
#questions = Question.all
#ans = Answer.where(user_id = current_user.id)
#answers = #questions.map { |q| [q, #ans.find_by_question_id(q.id)] }
View:
<% #answers.each do |q| %>
<%= q[0].question %> [<%= q[1].id %>] <%= q[1].score %>
<% [ 1, 2, 3, 4, 5 ].each do |f| %>
<%= radio_button_tag q[0].name, f, f == q[1].score %>
<% end %>
<br />
<% end %>
edit: oops... forgot the # on answers.each. fixed
<% #answers.each do |q| %>
<%= q[0].question %>
<% [1..5].each do |f| %>
<%= radio_button_tag 'score', f, (f == q[1].score).to_s %>
<% end %>
<br />
<% end %>
Its off the top of my head but basically you have to pass true or false not the number checked. I'm assuming that q[1].score is a number from 1 to 5
Hey, folks. Thanks for your help. Mike, you were close. There were two problems. First, the name "score" would be assigned to every button for every question. The name needs to be the same for every button for one question, but needs to change on the next question. See q[0].name below. Second, the score is numerical, not a string. So "to_s" resulted in every button being checked as it was made. Since there could only be one button checked per set, checking every button resulted in the last button being checked for each set. (e.g. the score 5 being checked each time)
<% #answers.each do |q| %>
<%= q[0].question %> [<%= q[1].id %>] <%= q[1].score %>
<% [ 1, 2, 3, 4, 5 ].each do |f| %>
<%= radio_button_tag q[0].name, f, f == q[1].score %>
<% end %>
<br />
<% end %>