Rails creating malformed routes with dots - ruby-on-rails

I'm using the path helper methods to generate URLs in link_to, and they are returning URLs formated like this :
http://localhost:3000/tweets.4
when I was expecting them to be formated like this:
http://localhost:3000/tweets/4
Note how it is using a dot as the delimiter instead of the expected forward slash. The top link doesn't resolve to the correct view, it simply reloads the /tweets view. When I manually edit the URL to be like the bottom, it opens the correct /tweets/show/.
The closest thing I found in my online research was that people encountered this with wrongly nested routing statements - but I don't think I'm doing that here.
I would appreciate any help or pointers anyone can provide!
Here are the related source files and version information :
tweets/index.html.erb
<h1>Listing tweets</h1>
<% #tweets.each do |tweet| %>
<div>
<!-- creates path in format of /tweets.2 -->
<div><%= link_to tweet.status, tweets_path(tweet) %></div>
<!-- creates path in the format of /user.1 -->
<div><%= link_to tweet.user.name, users_path(tweet.user) %></div>
</div>
<% end %>
tweets_controller.rb
class TweetsController < ApplicationController
def index
#tweets = Tweet.all
end
def show
#tweet = Tweet.find(params[:id])
end
def new
#tweet = Tweet.new
end
def create
#tweet = Tweet.new(params[:tweet])
#tweet.user = User.last
if(#tweet.save)
redirect_to :root
end
end
def edit
#tweet = Tweet.find(params[:id])
end
def delete
end
end
routes.rb
Zombietweets::Application.routes.draw do
resources :tweets
root :to => 'tweets#index'
end
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.9'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
end
group :assets do
gem 'sass-rails', '3.2.3'
gem 'coffee-rails', '3.2.1'
gem 'uglifier', '1.0.3'
end
gem 'jquery-rails', '2.0.2'
I'm using Rails 3.2.9 and Ruby 1.9.3p327 (2012-11-10) [x86_64-darwin12.2.0]

Have you tried tweet_path and user_path ?
You want to access the show action. For that action, the model name must be singular in the *_path call.
To be sure, try a rake routes in a console.
EDIT:
You also forget to add resources :users in your routes file :)

Related

Rails active admin undefined methods

I'm using this code:
ActiveAdmin.register_page "Dashboard" do
section "Recent Posts" do
table_for Post.order("id desc").limit(15) do
column :id
column "Post title", :title do |post|
link_to post.title,[:admin,post]
end
column :category,sortable: :category
column :created_at
end
strong (link_to "Show all posts")
end
end
and I get this error:
undefined method `section'
if I delete 'section' do-end part, then I get error for:
undefined method `table_for'
and so on...
Seems like I cant use any of active admin given methods, maybe I'm mising something? Any gems or something? I installed active admin gem using this setup:
gem 'inherited_resources', github: 'activeadmin/inherited_resources'
gem 'activeadmin', github: 'activeadmin'
gem 'devise', github: 'plataformatec/devise'
I'm using rails 5
I managed to transform my code and now it compiles without any errors.
ActiveAdmin.register_page "Dashboard" do
content :title => proc{ I18n.t("active_admin.dashboard") } do
columns do
column do
panel "Recent Posts" do
table_for Post.order("id desc").limit(5) do
column :name do |post|
link_to post.title, [:admin, post]
end
column :created_at
end
strong (link_to "Show All Posts" , :posts )
end
end
end
end
end
I suppose my previously used syntax is old and not supported any more.

RoR HTML template to .docx

I need to create a .docx file from a HTML template, so I used htmltoword gem.
Usage:
I added the gem (Gemfile):
gem 'htmltoword', '~> 0.5.1' #last version of the gem
I put a route (route.rb):
get 'preview' => 'foo#preview'
And in my bar.html.erb I have a link which target's that url:
<%= link_to '.docx', preview_path %>
Template (preview.docx.erb):
<h1>foobar</h1>
And in the controller (foos_controller.rb):
class FoosController < ApplicationController
respond_to :docx
#other code
def preview
respond_to do |format|
format.docx do
render docx: 'foobar', filename: 'preview.docx'
end
end
end
end
However, I'm getting an error:
ActionController::UnknownFormat
How to fix this error?
My config:
RoR v4.2.4
Ruby v2.2.3p173
Also, there is an open github issue for this/similar topic.
Update: as #kajalojha mentioned, respond_with / Class-Level respond_to has been removed to an individual gem, so I installed the responders gem, however, I get the same error.
Since respond_to has been removed from rails 4.2 to a individual gem i will recommend you to use formatter gem..
For further details you can look to the link given below.
Why is respond_with being removed from rails 4.2 into it's own gem?
Have you tried caracal-rails? You can find it here
I had to build this same functionality in an app earlier this year and also used the htmltoword gem.
# At the top of the controller:
respond_to :html, :js, :docx
def download
format.docx {
filename: "#{dynamically_generated_filename}",
word_template: 'name_of_my_word_template.docx')
}
end
I then have two "view" files that come into play. The first, is my method view file download.docx.haml. This file contains the following code:
%html
%head
%title Title
%body
%h1 A Cool Heading
%h2 A Cooler Heading
= render partial: 'name_of_my_word_template', locals: { local_var: #local_var }
From there, I have another file name_of_my_word_template.docx.haml that contains the meat of my Word file.
%h4 Header
%h5 Subheader
%div= local_var.method
%div Some other content
%div More content
%div Some footer content
When someone hits my_app.com/controller_name/download.docx, a Word file is generated and downloaded for them.
In order to ensure this happens, I have a route for the download method in my routes.rb file:
resources :model_name do
member do
get :download
end
end
Apologies for the long reply ... this has worked well for me and I hope helps you through this issue!
So, I figured it out. I added format: 'docx' to the route and it works now.
Note: as #kajalojha mentioned, respond_with / Class-Level respond_to has been removed to an individual gem, so I installed the responders gem.
Let's create a download logic.
Gemfile
gem 'responders'
gem 'htmltoword', '~> 0.5.1'
routes.rb
get 'download' => 'foos#download', format: 'docx' #added format
foos_controller.rb
class FoosController < ApplicationController
respond_to :docx
def download
#bar = "Lorem Ipsum"
respond_to do |format|
format.docx do
# docx - the docx template that you'll use
# filename - the name of the created docx file
render docx: 'download', filename: 'bar.docx'
end
end
end
end
download.docx.erb
<p><%= #bar %></p>
And I've added some link to trigger the download logic:
<%= link_to 'Download bar.docx', foo_download_path %>
Which will download the bar.docx file with "Lorem Ipsum" in it.

error: undefined method `presigned_post'

I'm writing a rails application with which a user can upload images. I am deploying with Heroku, and using Carrierwave and S3 to upload and store images. I have followed this heroku guide step-by-step...unfortunately I am still getting an error "undefined method `presigned_post'", and do not know how to resolve it. It seems the S3_BUCKET is not being recognized as an aws object...
Has anyone come across this problem and figured it out? Here's some code for reference:
Pictures controller:
class PicturesController < ApplicationController
before_action :set_s3_direct_post, only: [:new, :create]
def index
#pictures = Picture.all
end
def new
#pictures = Picture.all
#picture = Picture.new
end
def create
#picture = Picture.new(picture_params)
if #picture.save
redirect_to new_picture_path, notice: "You just uploaded a picture!"
else
render "new"
end
end
...
def picture_params
params.require(:picture).permit(:attachment)
end
private
def set_s3_direct_post
#s3_direct_post = S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}/${filename}", success_action_status: '201', acl: 'public-read')
end
end
New picture view:
<h1>Upload a new picture</h1>
<br>
<div class="well">
<%= form_for #picture, html: { class: 'directUpload', data: { 'form-data' => (#s3_direct_post.fields), 'url' => #s3_direct_post.url, 'host' => URI.parse(#s3_direct_post.url).host } } do |f| %>
<%= f.file_field :attachment %>
<%= f.submit "Upload", class: "btn btn-default" %>
<% end %>
</div>
And config/environment.rb:
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
# S3
S3_BUCKET='fotoes'
AWS_ACCESS_KEY_ID='secretxxxxxxxx'
AWS_SECRET_ACCESS_KEY='xxxxxxxsecretxxxxxx'
Any thoughts?
#Jillian I suppose your expectation was that S3_BUCKET class should call presigned_post method which is supposed to be defined. However, it seems its not. I took a look at the heroku page containing the tutorial you followed and well you followed every instruction. I suggest you contact heroku about the documentation. However, I would keep looking into it
Thank you all for your help. In the end I found a different walk-through that was much simpler and effective. (The Heroku one was a little complicated and left a lot to go wrong - go figure.)
This solved everything :)
edit:
not everything - had to complete one last step before I got the site running. Run this line in the terminal: $ heroku config:add AWS_ACCESS_KEY=value
and $ heroku config:add AWS_SECRET_KEY=value, where the value is your S3 credentials for each, respectively.
This was the only combo of fog, carrierwave, rails, fog-aws gems that worked (after weeks of fussing with them):
gem 'rails', '4.1.0'
gem 'carrierwave', '~> 0.10.0'
gem 'fog', '1.34.0'
gem 'fog-aws', '0.7.6'
i resolved this by updating my version of aws-sdk
$ bundle update aws-sdk

Instance variable not passed into view in Rails

EDIT: Solved. Solution:
Include
gem 'rabl' and gem 'oj' in your gemfile along with gem rabl-rails
For some reason, my instance variable isn't being passed into the view (I'm using Rabl).
Here's the relevant code:
articles_controller.rb
class ArticlesController < ApplicationController
respond_to :json, :xml
def index
#articles = Article.original.last(100)
end
def after
#articles = Article.where("id > #{params[:id]}")
end
def show
#article = Article.find(params[:id])
end
end
show.json.rabl
object #article
attributes :id, :headline, :source, :link
attributes :similar_articles => :similar
The error:
RuntimeError in Articles#show
Showing /Users/chintanparikh/Dropbox/Projects/Current/article_aggregator/app/views/articles/show.json.rabl where line #2 raised:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Any ideas?
The ID you're using to look up the record does not match one within your database.
Check what params[:id] is evaluating to and ensure that you can locate a record using that value.
Found the solution, you need these three lines in your Gemfile:
gem 'rabl-rails'
gem 'rabl'
gem 'oj'
Before, I only had rabl-rails, and I assume that was causing the problem.

Rails3 - will_paginate plugin strange output

I have the will_paginate plugin working in an application, but when I paginate a resource it just spits out the HTML as text, doesn't provide links to the next pages and such.
And when I manually type in the URL the plugin is working it just doesn't make <%= will_paginate #products %> into links such as next 1 2 3 ... last
This is the output
<span class="disabled prev_page">&laquo; Previous</span> <span class="current">1</span> 2 Next &raquo;
controller:
def index
#products = Product.all.paginate :per_page => 5, :page => params[:page]
#product_categories = ProductCategory.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #products }
end
end
view
<%= will_paginate #products %>
<%= will_paginate %> #for some reasons this works too
source 'http://rubygems.org'
gem 'rails', '3.0.0.beta2'
gem "will_paginate", '3.0.pre'
if you run into troubles related to haml we use that version:
gem 'haml', '3.0.2'
will_paginate is now at this location:
gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :branch => "rails3"
update your gemfile
I believe the reasons is the ways rails3 escapes html and for whatever reason will_pagiante is getting escaped.
to fix this you first need to get the correct gem as the plugin won't work so add gem 'agnostic-will_paginate', :require => 'will_paginate' and that is done in the new gem file located in the app folder of a rails3 project.
After that you need to stop rails from escaping will_paginate with raw so something like
<%=raw will_paginate #products %> which is the opposition of <%=h will_paginate #products %> which in rails3 is equivalent to <%= will_paginate #products %>
WILL PAGINATE MOVED TO GITHUB. This repository is no longer updated.
It's recommended that you install the gem instead of a Rails plugin:
gem install will_paginate
and Try Again

Resources