Rails: Partial Rendering fails with ActiveModel-Object - ruby-on-rails

I am setting up a rails5 project using OrientDB as Database (ActiveOrient, https://github.com/topofocus/active-orient)
I have two Models "Basiswert" and "Aktie", both provide a method "to_partial_path" and "to_parm" and should be qualified for partial-rendering
fast = Basiswert.like 'fast*'
fast.first.to_partial_path
=> "basiswerte/basiswert"
fast.first.aktie.to_partial_path
=> "aktien/aktie"
Unfortunately
<%= render #basiswert.aktie if #basiswert.aktie.present? %>
fails with the following error
ActionView::Template::Error ('["id", 134]' is not an ActiveModel-compatible object. It must implement :to_partial_path.)
Any suggestion where to look to solve this issue?
EDIT:
Further Investigation reveals that it's possible to render collections,
i.e
<%= render [#basiswert.aktie] %>
renders the view correctly. Rendering the plain Object still fails with the error-message shown above.

Related

Rendering an image from the Unsplash API in Rails

I have extensively researched this matter both on Stack Overflow and Google but found nothing conclusive. Since I'm completely new to the concept of API usage within Rails I have to ask for some advice.
I have followed the procedure from the github page
I have included the Unsplash helper in application_helper.rb as follows
def show_photo
Unsplash::Photo.find("tAKXap853rY")
end
and simply added
<%= image_tag show_photo %>
in my view.
This returns an object (So connectivity is good)
<img src="/images/#<Unsplash::Photo:0x007fc4b2f953c0>" alt="#
<unsplash::photo:0x007fc4b2f953c0>">
I'm aware that Rails is looking for a picture in the assets/images folder
How do I parse the inbound JSON and render it in my Rails view?
You can access to the urls key within the OpenStruct attributes in the Photo object that includes the raw, full, regular, small and thumb sizes, also as keys.
So, just to test you could use the raw one, like:
<%= image_tag Unsplash::Photo.find('tAKXap853rY')[:urls][:raw] %>
Or I think you could modify your method to accept one parameter which is the size key of the image, like:
module ApplicationHelper
def show_photo(size)
Unsplash::Photo.find("tAKXap853rY")[:urls][size.to_sym]
end
end
Then:
<%= show_photo('raw') %> # 'full', 'regular', etc ...
further to this solution I am trying to display the photographer's name by using the user.name method.
In the console I can get the following :
photo = Unsplash::Photo.find("tAKXap853rY")
photo.user.name
will return
=> "Alejandro Escamilla".
But in RAILS :
def show_photo(size)
#photo =Unsplash::Photo.find("tAKXap853rY")[:urls][size.to_sym]
end
just trying to display the name in my view like:
<%= #photo.user.name %> will return "user undefined method".
The .user.name is accessible in the console but not in rails! What have I missed? Thanks

Devise: Missing partial when accessing edit registration

Im trying to render a collection of user inboxes:
<%= render current_user.my_inbox %>
It works fine with different views/controller except when i attempt to access the devise edit registration.
Missing partial devise/inboxes/_inbox
I notice that Devise is not looking at the right path. how do i tell devise that my inbox partial is located at inboxes/_inbox not devise/inboxes/_inbox
any suggestions will be greatly appreciated.
Assign the partial's name manually and pass along the data.
<%= render partial: "inboxes/inbox", collection: current_user.my_inbox %>
More info in the Rails Partial Docs (specifically passing variables to a given partial)

Missing partial comments/_comment in rails

I'm using a tutorial to get a feel for ruby, I am very much a beginner. Before posting this I have spent a couple of hours trying to resolve this myself with no luck. Sorry in advance if my explanation isn't great:
So I am getting this error screen:
error message
I am following a tutorial to make a basic reddit style app and I am trying to add the comments functionality.
When you render a collection like render #comments, Rails will check the type name of the items in #comments (i.e. 'comment') then look for a partial under app/views/comments/_comment.html.erb by default (note the plural singular distinction between the partial name and the folder/collection name).
The following steps should resolve your issue:
Create a comments partial under:
app/views/comments/_comment.html.erb
Now, when you call render #comments, each item of your collection is passed to the partial as a local variable as the same name without the underscore:
In _comment.html.erb
<%# comment is defined because it matches the name of the partial %>
<%= comment.<some_attribute_on_comment> %>

Ruby on rails flash notice error

I have a problem with flash[:notice] = "Message" in Ruby on Rails.
I am trying to create login fault error message. My login fault handling is:
flash[:notice] = "Invalid username/password combination."
redirect_to(:action => 'login')
For the reason I don't know, alert just doesn't show up. I have red tons of possible solutions, but all of them just doesn't work for me. I am using Safari / Google Chrome web browsers.
Your controller code looks fine. I suspect your problem is not that you are calling the flash method incorrectly in your controller, but rather that you did not write the code (correctly) to display the flash in your view.
flash simply holds a message for your views to display. You are setting the message correctly, but you may not be displaying it properly. I can't tell because you aren't posting your view code.
For example, if this action is the result of submitting a user/login.html.erb form, based on your controller code, you would want to have the following code inside that view file:
<% flash.each do |key, value| %>
<div class="alert"><%= value %></div>
<% end %>
This code is a more basic version of what is described in this article.

render erb from database into view problem please help!

i am saving some erb in my database and rendering it in the view like this:
erb = ERB.new(content)
render :text => erb.result
I am getting errors when trying render erb that has the image_tag in the erb saved in the database. The error is :
undefined method `image_tag' for main:Object
Anyone help on this ? i also get the error with the stylesheet_link_tag ?
Thank alot
rick
I think that you would need to pass the optional binding parameter to the ERB::render method. This effectively provides the local variables in the scope of the ERB template. In other words the binding needs to provide the image_tag variable to the template.
I don't know what 'content' is in your case but the following will pass the binding from the 'parent' view assuming that #obj.image_tag is visible from that view:
<%= ERB.new("image tag - \<\%= #obj.image_tag \%\>").result(binding) %>
It's because you haven't helper in your controller. You need include all Helper do you use.

Resources