RoR undefined method - ruby

Ive had a problem for a couple of days. I've tried every solotion thad i found on stackoverflow, but nothing worked...
Rails gives me an error when i try to add a new 'journey' (just a simple datapase entry): undefined method `journeys_index_path' for #<#:0x3d0b888>
Problem resolved!
The issue was the model name. It was Journeys but it should be journey ! The only thing I had to do is rename the modelfile in app/models. (i had to change the filename and the contents, pretty simple!
Oh and the model name is not the same as the database name.
Please check http://rubyonrailsthrissur.wordpress.com/2012/07/18/naming-conventions-while-creating-tables-in-rails/ for more naming rules
Routes.rb:
Fabulam::Application.routes.draw do
root "home#home"
resources :journeys
match 'auth/:provider/callback', to: 'sessions#create', via: 'get'
match 'auth/failure', to: redirect('/'), via: 'get'
match 'signout', to: 'sessions#destroy', via: 'get', as: 'signout'
end
journeys_controller (relevant part)
def index
#current_user ||= User.find(session[:user_id]) if session[:user_id]
#journey = Journeys.where(uid: current_user.uid)
end
def show
#journey = Journeys.find(params[:id])
end
def new
#journey = Journeys.new
end
new.html.erb (location = journeys/new.html.erb)
<!DOCTYPE html>
<html>
<body>
<div>
<h1> Maak hieronder een nieuwe reis aan! </h1>
<% form_for(#journey) do |f| %>
<p>
<%= f.label :name %><br/<
<%= f.text_field :name %>
</P>
<p>
<%f.submit "Dubmittt" %>
</p>
<% end %>
</div>
</body>
</html>
index.html.erb (location journeys/index.html.erb, this one is working fine)
<!DOCTYPE html>
<html>
<body>
<div id="user_nav">
<% if current_user %>
Signed in as <strong><%= current_user.name %></strong>!
<%= link_to "Sign out", signout_path, id: "sign_out" %>
<% else %>
<%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in" %>
<% end %>
</div>
<div id="travels">
Hieronder al je reizen!
<% #journey.each do |journey| %>
<h2><%= link_to journey.name, journey %></h2>
<% end %>
</div>
<p><%= link_to "Add a new journey" , new_journey_path %> </P>
</body>
</html>
Rake routes
Prefix Verb URI Pattern Controller#Action
root GET / home#home
journeys GET /journeys(.:format) journeys#index
POST /journeys(.:format) journeys#create
new_journey GET /journeys/new(.:format) journeys#new
edit_journey GET /journeys/:id/edit(.:format) journeys#edit
journey GET /journeys/:id(.:format) journeys#show
PATCH /journeys/:id(.:format) journeys#update
PUT /journeys/:id(.:format) journeys#update
DELETE /journeys/:id(.:format) journeys#destroy
GET /auth/:provider/callback(.:format) sessions#create
auth_failure GET /auth/failure(.:format) redirect(301, /)
signout GET /signout(.:format) sessions#destroy
Am I missing something?
Thanks in advance!

Related

How to update feed using ajax in Rails 4.0

I am trying to create an ajax functionality for a sample app of michael heart book . I created a feed model controller which is micropost in hear book . I am trying to use ajax like post feed method . But I am getting some error . I am attaching my code below take a look and help me .
#feeds_controller.erb
class FeedsController < ApplicationController
before_action :authenticate_user! ,only: [:create, :destroy]
def create
#feed = current_user.feeds.build(feed_params)
if #feed.save
flash[:success] = "Micropost created!"
#redirect_to root_url
respond_to do |format|
format.html { redirect_to root_url }
format.js
else
#feed_items = []
render 'static_pages/home'
end
end
def destroy
end
private
def feed_params
params.require(:feed).permit(:content)
end
end
And my home page is rendering some partials Here is the code :
#static_pages/home.html.erb
<% if user_signed_in? %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= render 'shared/user_info' %>
</section>
<section class="stats">
<%= render 'shared/stats' %>
</section>
</aside>
<div class="col-md-8">
<h3>Micropost Feed</h3>
<section class="micropost_form">
<%= render 'shared/feed_form' %>
</section>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<% end %>
Here are two codes Everything work perfectly but when I am trying to add ajax I get the error which is :
Rendered shared/_feed.html.erb (3.1ms)
Rendered feeds/create.js.erb (6.0ms)
Completed 500 Internal Server Error in 73ms (ActiveRecord: 44.4ms)
NoMethodError (undefined method `any?' for nil:NilClass):
app/views/shared/_feed.html.erb:1:in `_app_views_shared__feed_html_erb___516512175__638262078'
app/views/feeds/create.js.erb:1:in `_app_views_feeds_create_js_erb___967625174__637838198'
app/controllers/feeds_controller.rb:9:in `create'
To execute ajax on create method I added these files unders feeds views file name is create.js.erb
$("#feed_add").html("<%= escape_javascript(render partial: "shared/feed") %>");
And form file is under shared view file with name _feed_form.html.erb
<%= form_for(#feed,:remote => true) do |f| %>
<div id="feed_add">
<%= f.text_area :content, placeholder: "Compose new feed..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
#_feed.html.erb
<% if #feed_items.any? %>
<ol class="microposts">
<%= render #feed_items %>
</ol>
<%= will_paginate #feed_items %>
<% end %>
So tell me how can I add feed and without refreshing how can I show feed using partial In this code .
Here you simply need to render one thing that is feed for that in micheal heart book you simply add this line of code inside your create ajax file here is the code :
$(".microposts").prepend('<%= j render #feed %>');
Here you can get the result what you want .

Using carmen gem with rails

What I am trying to do is show the 2 letter abbreviation code of the countries, i.e 'US'.
After the country is selected I need to show its state or province.
but I am having a problem.
My code looks like
<%= f.select :country_code, region_options_for_select(only_us_and_france) %>
and define this in helper:
def only_us_and_france
Carmen::Country.all.select{|c| %w{US FR}.include?(c.code)}
end
I am using Rails 4.1.0.
I have solved this by :
Step1: Generated migration
rails g migration addStateCodeFieldToAccounts state_code:string
Step2: Define a method inside controller
def subregion_options
render partial: 'subregion_select'
end
Step3: Declare in routes
resources :accounts do
collection do
get 'subregion_options'
end
end
Step4: In view
<div class="input-control select state_input" data-role="input-control">
<%= f.select :country, region_options_for_select(only_us_and_france) %>
</div>
<div class="input-control select state_input" data-role="input-control">
<%= render partial: 'subregion_select', locals: {parent_region: f.object.country} %>
</div>
Step5: Make partial subregion_select
<div id="account_state_code_wrapper">
<% parent_region ||= params[:parent_region] %>
<% country = Carmen::Country.coded(parent_region) %>
<% if country.nil? %>
<em>Please select a country above</em>
<% elsif country.subregions? %>
<%= subregion_select(:order, :state_code, parent_region) %>
<% else %>
<%= text_field(:order, :state_code) %>
<% end %>
</div>
Step6: In my js file written this
$('select#account_detail_country').change(function(){
selectWrapper = $('#account_state_code_wrapper')
countryCode = $(this).val()
url = "/account_details/subregion_options?parent_region="+countryCode
console.log(url)
selectWrapper.load(url)
});
yeah and it works :) Hope this will help you.

Rails 4 Error: undefined method[]' for nil:NilClass`

I'm trying to setup a messaging system in my site using the Mailboxer gem in Rails 4.
When my app gets to this code:
def fetch_params(key, *subkeys)
params[key].instance_eval do
case subkeys.size
when 0 then self
when 1 then self[subkeys.first]
else subkeys.map{|k| self[k] }
end
end
it returns the error: undefined method '[]' for nil:NilClass
I've tried to figure this out, but I'm not very experienced. I've been following this tutorial: http://jamestansley.com/2014/02/22/customizing-the-mailboxer-ruby-gem-2/
I tried removing the line of code returning the error just to see what would happen, after which I received the error wrong number of arguments (1 for 2..6).
Can anyone attempt to explain this code to me? Been having a really rough time implementing this feature.
view/conversations/_form:
<%= simple_form_for :conversation, url: :conversations do |f| %>
<%= f.input :recipients %>
<%= f.input :subject %>
<%= f.input :body %>
<div class="form-actions">
<%= f.button :submit, class: 'btn-primary' %>
<%= submit_tag 'Cancel', type: :reset, class: 'btn btn-danger' %>
</div>
<% end %>
view/conversations/show:
<h1><%= #conversation.subject %> </h1>
<!--may have to work on this view. don't know if i did the haml conversion correctly-->
<h1> <%= conversation.subject %> </h1>
<ul>
<% content_tag_for(:li, conversation.receipts_for(current_user)) do |receipt| %>
<%= message = receipt.message %>
<h3><%= message.subject %> </h3>
<p><%= message.body %> </p>
</ul>
<% render 'messages/form', conversation: conversation %>
<% end %>
And here's my github repo: https://github.com/portOdin/GoFavorIt-Heroku/blob/8cd19ff5b61eb805dc694a6586f50df608752be2/app/views/conversations/show.erb

Bring code from view into a popup in Rails

I want to bring this code from my view html into a popup when a link is clicked
Is that possible in Ruby on Rails? I already have the pop up working but I'm wondering about the code to show just the comments:
<div class = "comments"><% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= image_tag("http://www.gravatar.com/someavatarlink %) <!-- Retrieves Gravatar -->
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %></div>
Added Ajax call to _comment_form.html.erb
<%= link_to "Link", comment, :remote => true %>
Comments
<% end %></div></div>
<div id ="modal" class = "comments"><% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %></div>
Added def show into comments controller
class CommentsController < ApplicationController
def new
#post = post.new(params[:post])
end
def show
#comment = Comment.find(params[:id])
respond_to do |format|
format.js
end
def create
#post = post.find(params[:micropost_id])
#comment = Comment.new(params[:comment])
#comment.post = #post
#comment.user = current_user
if #comment.save
redirect_to(:back)
else
render 'shared/_comment_form'
end
end
end
Created show.erb.js and put it into 'comments' and 'shared' folders
$("#popup").html('<%= escape_javascript(render "comments") %>');
Then finally wrote my partial which is in comments/_comment.html.erb
<% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %>
1. Ajax call
To retrieve the data you use an Ajax call.
<%= link_to "Link", comment, :remote => true %>
Rails will evaluate these requests and will look for a .js view first (it will use .html if it does not exist).
Make also sure that the controller accepts requests to .js like
def show
#comment = Comment.find(params[:id])
respond_to do |format|
format.js
end
end
2. write js view
Add a show.erb.js view to your Comments . This is a JavaScript file with ERB evaluation.
In this template use your js popup code and tell it to fill a div with your html code like so:
$("#popup").html('<%= escape_javascript(render #comment) %>');
This will render the comment. The only thing we need then is a partial to render the html of the comment.
3. write partial for html
Write a partial for the view part you want to have in the popup. This can then be used in a normal html view or the js view. To make it work with the code above call it _comment.html.erb
To know more about partials you can check the guides here:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

my final step in the rails app, how to make comments appear in a popup?

This is the last remaining item to complete my first rails app and need some help.
On each user profile (localhost:3000/users/username), there's a listing of posts that the user has made. Associated with each post are comments. So post_id: 3 could have comments.
I have it working already in view form but I need the comments to appear in a popup instead when the "Comments" link under each post is clicked.
I have already applied facebox which is a jQuery-based lightbox that displays popups.
I just need to move what's currently shown in show.html.erb into a popup.
There's the _comment_form.html.erb which renders into _post.html.erb
<%= link_to #, :rel => "facebox-#{post.id}" do %>
+<%= post.comments.count.to_s %>
<% end %>
<div class ="ItemComments"><% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= image_tag("http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5::hexdigest(comment.user.email)}" %>
<span class="users"><%= link_to comment.user.name, comment.user %></span>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %></div>
The above renders into _post.html.erb using:
<%= render 'shared/comment_form', post: post if signed_in?%>
Then it renders into show.html.erb
I'm trying to use this line, but what do I link it to?
<%= link_to #, :rel => "facebox-#{post.id}" do %>
+<%= post.comments.count.to_s %>
<% end %>
This is shared/_comment.html.erb
<% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= image_tag("http://www.gravatar.com/avatar.php?gravatar") %>
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %>
One way of doing this is to render your comments into a hidden div and give that div an id. Next you point your link to the id of the div using # followed by the id. It would look something like this:
_post.html.erb
<%= link_to "#comments", :rel => "facebox" do %>
<%= post.comments.count.to_s %>
<% end %>
<div id="comments">
<%= render 'shared/comment_form', post: post if signed_in?%>
</div>
CSS
#comments {
display: none;
}
See the 'Divs' heading over at the Facebox docs.

Resources