Getting best_in_place gem to co-operate with Redcarpet markdown - ruby-on-rails-3.1

For my markdown text i normally do the following on the text content(#user.content):
<%= raw Redcarpet.new(#user.content).to_html.html_safe %>
However i am now using the best_in_place gem like this
<%= best_in_place(#question, :statement, :path => {:controller => "users", :action => "update"}, :type => :textarea) %>
How can i integrate Redcarpet with this? i have tried this:
<%= Redcarpet.new(best_in_place(#question, :statement, :path => {:controller => "users", :action => "update"}, :type => :textarea)).to_html.html_safe %>
however the output is not what is expected. The best_in_place readme seems to point to a way to solve this, but i don't understand it here, under the section titled "Using custom display methods". Can someone explain to me how i can make this work?

I have never used the best_in_place gem, so I don't have tried it out. But from the documentation, you should do the following steps:
Add to the call to best_in_place the additional argument :display_as.
Implement the method referenced by :display_as in your model object.
Here are the details:
Additional argument
%= best_in_place(#question, :statement, :display_as => 'mk_statement', :path => {:controller => "users", :action => "update"}, :type => :textarea) %>
Implementation in the model
class Question
require 'redcarpet'
def mk_statement
Redcarpet.new(self.statement).to_html.html_safe
end

You can also use the display_with attribute and pass it a proc like so:
<%= best_in_place(#question, :statement, :display_with => proc { |v| Redcarpet.new(v).to_html.html_safe, :path => {:controller => "users", :action => "update"}, :type => :textarea) %>

Related

Download link on Paperclip Attachment in Rails_admin

I am using rails_admin gem together with Paperclip. My model looks like this:
class Product < ActiveRecord::Base
has_attached_file :asset,
:styles => {
:thumb => "100x100#",
:small => "150x150>",
:medium => "200x200" }
validates_attachment_content_type :asset, :content_type => /\Aimage\/.*\Z/
end
How can I include a download link into the index action? So that, on admin/products every entry in the table will have a download link? I read through the documentation, but they don't seem to specify any of these features.
[EDIT]
On my main index action which was routed here: /products I used to do:
<%= link_to "Download", product.asset.url(:original, false) %>
You just need to do.
<%= link_to "Download", product.asset(:original) %>
or
<%= link_to "Download", product.asset.url(:original) %>
They both do the same thing.
If you want to change what version of the image they download just change :original to :medium, :small or :thumb.
For Rails Admin do following:
config.model "Product" do
list do
....
field :download do
formatted_value do
bindings[:view].tag(:a, href: bindings[:object].assets(:original)) << "Download"
end
end
end
...
end
[SOLVED]
Submission model:
class Submission < ActiveRecord::Base
# Image attachment and validations
has_attached_file :file,
:url => "/files/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/files/:class/:attachment/:id/:style/:basename.:extension"
validates_attachment_content_type :file, :content_type => 'application/pdf'
end

Capybara selecting option from drop down

I have reviewed a number of solutions from stackoverflow to no avail. I was hoping someone could help me understand the error of my ways.
The haml markup looks like:
.graduation-date-range
%label Graduation Date Range
.row-fluid
.span6
.control-group
.controls
.bfh-selectbox{:id => "graduation_start_year", :style => "width: 100%"}
= f.hidden_field :graduation_start_date
%a.bfh-selectbox-toggle{"data-toggle" => "bfh-selectbox", :href => "#", :role => "button"}
%span.bfh-selectbox-option Start Year
%b.caret
.bfh-selectbox-options
%div{:role => "listbox"}
%ul.start-years{:role => "option"}
- (2012..2018).each do |y|
%li
%a{"data-option" => y, :href => "#"}= y
.help-inline
.span6
.control-group
.controls
.bfh-selectbox{:style => "width: 100%"}
= f.hidden_field :graduation_end_date
%a.bfh-selectbox-toggle{"data-toggle" => "bfh-selectbox", :href => "#", :role => "button"}
%span.bfh-selectbox-option End Year
%b.caret
.bfh-selectbox-options
%div{:role => "listbox"}
%ul.end-years{:role => "option"}
- (2012..2018).each do |y|
%li
%a{"data-option" => y, :href => "#"}= y
.help-inline
The solution I am currently trying to employ:
select('2012', :from => 'graduation_start_year', :visible => false)
I receive :
Capybara::ElementNotFound:
Unable to find select box "graduation_start_year"
My understanding is that either the label, name, or ID can be used to select from but I may be mistaken.
Thank you in advance to those who respond.
It appears that this is not a form select box, but an html list that has been turned into something that resembles a dropdown box via javascript and CSS.
capybara select is looking for <select> and <option> tags. This is going to require a js type test with selenium or poltergeist to test.

Rails 4.0.2 haml version of form_for drive me crazy

I am quite new to Rails world and I am trying to create a form for my ticket model
I have this following in my routes.rb
resources :tickets
In my tickets_controller, I have the following
def new
#ticket = Ticket.new
end
And in my tickets/new.html.haml
= form_for(#ticket, :url => { :action => "create" }) do |f| # <- this line is causing error
...
My error is syntax error, unexpected ')'
I don't understand cause similar template file is working. Also, if I try to write the following tickets/new.html.erb, it is working
<%= form_for(:ticket, :url => { :action => "create" }) do |f| %>
<% end %>
Could you please help me to understand what i am exactly doing wrong ?
Thank you very much !
I found your problem, one was related to indentation(if-block) and other was an extra comma(submit button).
%h1{:class => ["page-header"]} New Ticket
= form_for(#ticket, :url => { :action => "create" }) do |f|
= if #ticket.errors.any?
%h2 #{pluralize(#ticket.errors.count, "error")} + " prohibited this post from being saved:" ### code indentation done as it belongs to if block
%ul
= #ticket.errors.full_messages.each do |msg|
%li = msg
%p
= f.label :subject
= f.text_field :subject
%p
= f.label :body
= f.text_area :body
%p
= f.submit :class => "btn btn-primary" ### Removed comma
= link_to 'Back', tickets_path

omniauth identity not working on heroku with simple_form_for

I'm having trouble with Heroku not playing ball and throwing me this error
ActionController::RoutingError (No route matches [POST] "/auth/identity/register"):
I have the following working in dev.
model/user.rb
class User < OmniAuth::Identity::Models::ActiveRecord
has_many :services
controllers/users_controller.rb
def new
#user = env['omniauth.identity'] || User.new
end
users/new.html.erb
<%= simple_form_for #user, url: "/auth/identity/register" do |f| %><fieldset>
<legend><%= controller.action_name.capitalize %> User</legend>
<%= f.input :name, input_html: { name: "name" } %>
<%= f.input :email, input_html: { name: "email" } %>
<%= f.input :password, input_html: { name: "password" } %>
<%= f.input :password_confirmation, input_html: { name: "password_confirmation" } %>
<div class="form-actions">
<%= f.button :submit %>
<%= link_to 'Cancel', users_path, :class => 'btn' %>
</div></fieldset><% end %>
routes.rb
match "/auth/:service/callback" => 'services#create'
match "/auth/failure" => 'services#failure'
resources :users
This all works Perfectly on my machine but Heroku doesn't like it.
environments/development.rb and production.rb are the defaults created with "rails new..." with the following added:-
Rails.application.config.middleware.use OmniAuth::Builder do
require 'openid/store/filesystem'
provider :identity, fields: [:name, :email],
model: User,
on_failed_registration: lambda { |env|
UsersController.action(:new).call(env)
}
# generic openid
provider :open_id, :store => OpenID::Store::Filesystem.new('./tmp'), :name => 'openid'end
Hope this all makes sense and someone has the answer. Any and all help muchly appreciated.
Regards
Ok, I found the answer, just not sure why this is so though. Could be another question later if I don't find it here in stackoverflow.
Anyways, I was following this Excellent guide on www.communityguides.eu and put the omniauth configurations in development.rb and production.rb. Which made perfect sense as the OpenID::Store paths were different being:-
development.rb
OpenID::Store::Filesystem.new('/tmp')
production.rb
OpenID::Store::Filesystem.new('./tmp')
The Answer...
Put the omniauth configurations in initializers/Omniauth.rb
This means I now have to change this file every time I push to Heroku, but Heroku thanks me and allows my app to run nicely.
The question this raises for me (which I'm sure is answered elsewhere). What is the difference in the load order from Heroku and our Dev environments? Heroku appears to be loading initializers before the environment configs. And one more...anyone know a workaround that will save me having to update the Omniauth initializer file every time I want to push to Heroku ? :)
Thanx for reading and I hope this helps save someone some time.

Paperclip "Gem" Rails 3.1 undefined method model file name

Using rails 3.1.1, Ruby 1.9.2,
Gems: gem 'haml', gem 'simple_form', gem 'aws-sdk',
gem 'paperclip', :git => "git://github.com/thoughtbot/paperclip.git"
plugin: country_select: git://github.com/rails/country_select.git
Having an issue uploading/displaying images pushed to Amazon S3 through paperclip (GEM)
Error: undefined method `avatar_file_name' for #Player:0x00000102aff228
For the most part I was following the example on the git-hub page for paperclip
https://github.com/thoughtbot/paperclip
Here is what I have in my code:
Migration: 20111224044508_create_players.rb
class CreatePlayers < ActiveRecord::Migration
def change
create_table :players do |t|
t.string :first_name
t.boolean :first_name_public, :default => false
...
t.string :website
t.boolean :website_public, :default => false
t.has_attached_file :avatar
t.timestamps
end
end
end
Model: Player.rb:
class Player < ActiveRecord::Base
attr_accessible :first_name, ... :website
validates_presence_of :username, :email
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":class/:id/:style/:filename"
{Unrelated validations}
end
S3 file: s3.yml
development:
bucket: voh_development
access_key_id: *********************
secret_access_key: ********************
staging:
bucket: voh_staging
access_key_id: *********************
secret_access_key: ********************
production:
bucket: voh_production
access_key_id: *********************
secret_access_key: ********************
Controller: players_controller.rb
class PlayersController < ApplicationController
def create
#player = Player.create(params[:player])
if #player.save
redirect_to players_path, :notice => "Player Created";
else
render :action => 'new'
end
end
{basic restful}
end
Views:
Edit.html.haml + New.html.haml
= simple_form_for #player do |f|
= f.input :first_name
...
= f.input :website
= f..file_field :avatar
.input_div
= f.button :submit
index.html.haml
...
%td Avatar
%td First Name
...
%td Actions
- #players.each do |player|
%tr
%td
= image_tag #player.avatar.url(:thumb)
%td
= player.first_name
...
%td
= link_to ' Show ', player_path(player.id)
|
= link_to ' Edit ', edit_player_path(player.id)
show.html.haml
= image_tag #user.avatar.url
%br
= #player.first_name
...
Research:
I found a lot to do with the pluging and genration of the migration but it all seems old. Most of them suggest putting in the up down in the migration for the 4 attributes. However it seems that should have been replaced by the one line t.has_attached_file :avatar.
I have a rails 3.0 project and this worked. I am able to upload products and pull them back down. (had to play with the suggested image_tag #icon.avatar.url and turned it into %img{:src => URI.unescape(icon.icon.url)} but that a different question.)
TLDR: Fixed typo in index.html.haml from #player => player, Added :avatar to attr_accessible.
I woke up this morning and had a different error.
instead of: undefined method Avatar_file_name'
I got: undefined method avatar' for nil:NilClass
That error was caused buy a simple type in my code. I used an instance vairable instead of .each variable I should have been using (index.html.haml:9)
Now the app was not erring out but the file was still not uploading.
In the development log I found this. (I did not look here the first time I posted)
WARNING: Can't mass-assign protected attributes: avatar
I then went and added :Avatar to attr_accessible and everything started working.
Not sure if this is supposed to be required or not but I did see that they had updated S3 header to be a proc yesterday.
https://github.com/thoughtbot/paperclip/tree/master/test/storage
I am not going to close this out yet. There will be an edit because I am going to play with the version and quickly report my findings today. Then I will close this out.
Edit:
I tryed switch back to 2.4.5 and I am getting the error that made me switch to pulling master in the first place. When attempting to do a migration with t.has_attached_file :avatar it fails to migrate and gives the following error.
undefined method `has_attached_file' for #ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition:0x00000105053600
I think I will stick with pulling from master.

Resources