So I'm getting the error stated in the title. What code would I have to write in my Postscontroller to fix this? I'm not sure what I would have to do here, would I have to define rsvp in my post controller? If thats the case how would I go about doing that?
class RsvpController < ApplicationController
def create
rsvp = current_user.rsvps.build({post_id: params[:id]})
if rsvp.save
end
end
end
Show.html.erb
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>where:<%=#post.longitude %>, <%=#post.latitude%></p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User_id:</strong>
<%= #post.user_id %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<%= button_to "Rsvp now", rsvp_post_path(#post), class: "btn btn-primary" %>
routes.rb
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :posts
devise_for :users
root 'home#index'
get 'home/ruby_meetup'
resources :posts do
post 'rsvp', on: :member
end
Also I want it so it shows the list of people who already rsvped for the event. How would I go about doing that? I would appreciate some help as I'm still learning rails and this is my first project.
By the following line, you define the route:
resources :posts do
post 'rsvp', on: :member
end
By this line you're requesting this route:
To process the request, you need a controller action rsvp, which actually the error states.
Thus, just define the action (method) and process the request:
class RsvpController < ApplicationController
def create
rsvp = current_user.rsvps.build({post_id: params[:id]})
if rsvp.save
end
end
def rsvp
# process the request here...
end
end
Related
I'm trying to make a dropdown search by category feature on my webapp. I am currently trying to get the dropdown menu to work, but I am constantly getting the error saying:
First argument in form cannot contain nil or be empty
Here is the view:
<%= form_for #search, html: {class: "pure-form"} do |s| %>
<div class="field">
<%= s.label :category %>
<%= s.select(:category, options_for_select([['Other', 'Other'], ["Breakfast", "Breakfast"], ["Lunch/Dinner", "Lunch/Dinner"], ["Dessert", "Dessert"]])) %>
<%end%>
<div class="card-columns">
<% #images.each do |image| %>
<div class="card">
<div class="card-body p-1">
<%= image_tag image.image, class: 'd-block w-100' %>
<%= link_to image.title, image_path(image.id), class: "btn btn-primary stretched-link" %>
</div>
</div>
<% end %>
</div>
And here is the controller:
class SearchesController < ApplicationController
def new
#search = Search.new
#categories = Image.uniq.pluck(:category)
end
def create
#search = Search.create(search_params)
redirect_to #search
end
def show
#search = Search.find(params[:id])
end
private
def search_params
params.require(:search).permit(:category)
end
end
And here are the routes:
Rails.application.routes.draw do
devise_for :users
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root 'pages#home'
get '/images', controller: 'images', action: 'recent'
get '/upload', controller: 'images', action: 'new'
get '/#:username', to: 'users#show', as: :profile
delete 'images/:id(.:format)', :to => 'images#destroy'
resources :images, only: [:index, :show, :create]
get 'searches/search'
resources :searches
end
I tried to configure different things to see if I could get it working, but I could not. I am very unclear as to what is the problem. Any help would be appreciated! Thanks in advance!
I am new bee in rails and trying to figure out how to pass id using custom controller but it look a blocker to me.
below is code of controller :
class UploadsController < ApplicationController
def index
#upload = Upload.new
#uploadimage = Upload.all
end
def home
end
def destroy
end
end
below is the index.html.erb file
<h1>Upload#index</h1>
<p>Find me in app/views/upload/index.html.erb</p>
<%= form_for(#upload, url: "/uploads/home", :html => {:class => 'dropzone'} ) do |f| %>
<div class="fallback">
<input name="file" type="file" multiple />
</div>
<% end %>
<h3>Uploaded images</h3>
<% #uploadimage.each do |image| %>
<% if !image.nil? %>
<%= image_tag("/images/#{image.name}",style: 'height:100px; width:100px;') %>
<%= link_to "delete #{image.name}", uploads_url(image) %>
<% else %>
<p>No images are present in db</p>
<% end %>
<% end %>
below is routes.rb
Rails.application.routes.draw do
get "uploads", to: "uploads#index", via: :get
get "uploads/:id", to: "uploads#destroy", via: :get
get "uploads/index/:id", to: "uploads#destroy", via: :get
get "uploads/index/", to: "uploads#index", via: :get
root "uploads#index"
end
how to pass id to destroy/home action as i m getting id as dot instead of slash
Why you are using custom routes, if rails providing RESTful routes using resources
Rails.application.routes.draw do
resources :uploads
root "uploads#index"
end
Edited:
Rails.application.routes.draw do
resources :uploads, :controller => "custom_controller"
root "uploads#index"
end
This will manage all 7 action, and check rake routes for path for this all 7 action.
I am building a basic website with forum functionality. I am trying to implement a feature in the PostController#Show that will display each comment associated with that post. However, I keep getting an error along the lines of Can't Find ______ Without An ID. Here is what I have:
Posts Controller
class PostsController < ApplicationController
def show
#topic = Topic.find(params[:topic_id])
#post = Post.find(params[:id])
#comments = Comment.find(params[:post_id])
end
I have tried multiple variations for #comments, but similar errors occur.
Post Show View
<h1><%= markdown_to_html #post.title %></h1>
<div class="row">
<div class="col-md-8">
<small>
<%= image_tag(#post.user.avatar.tiny.url) if #post.user.avatar? %>
submitted <%= time_ago_in_words(#post.created_at) %> age by
<%= #post.user.name %>
</small>
<p><%= markdown_to_html( #post.body) %></p>
<p><%= markdown_to_html( #comments ) %></p>
</div
<div class="col-md-4">
<% if policy(#post).edit? %>
<%= link_to "Edit", edit_topic_post_path(#topic, #post), class: 'btn btn-success' %>
<% end %>
</div>
</div>
Routes
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index] do
resources :comments, only: [:create]
end
end
get 'about' => 'welcome#about'
get 'contact' => 'welcome#contact'
root to: 'welcome#index'
end
I assume this has something to do with nesting Comments within Posts within Topics and not using the correct syntax. Can anyone point me in the right direction?
Thank you,
Matt
Couldn't you just do, in your view:
#post.comments.each do |comment|
Alternatively in your controller:
#comments = #post.comments
I'm assuming you have an association set up.
Ruby on rails application have two model User and Location
User model
belongs_to :location
Location model
has_many :users
routes.rb
devise_for :users
resources :users
This view in location show page
<% #location.users.each do |locuser| %>
<%= link_to locuser.email, user_path %><br>
<% end %>
Error is Couldn't find User with 'id'
This my Users Controller
def show
#user = User.find(params[:id])
end
I added <%= link_to locuser.email, current_user %>
it also not working.
I am considering you are not using devise routes. where no need to pass user object to routes. In your show action you do need id so I am passing user object for show path. If this is not the case run rake routes | grep user and paste log here
it should be user_path(locuser)
try change :
<% #location.users.each do |locuser| %>
<%= link_to locuser.email, user_path(locuser) %><br>
<% end %>
or
<% #location.users.each do |locuser| %>
<%= link_to locuser.email, locuser %><br>
<% end %>
I am busy going through PBP - Agile Web Development with Rails and implementing the locale switcher.
However when I try switch between english and spanish I get a error:
No route matches [POST] "/en"
My controller is as follows:
class StoreController < ApplicationController
skip_before_filter :authorize
def index
if params[:set_locale]
redirect_to store_path(locale: params[:set_locale])
else
#products = Product.order(:title)
#cart = current_cart
end
end
end
and an extract of the application.hmtl.erb that is being used;
<div id="banner">
<%= form_tag store_path, class: 'locale' do %>
<%= select_tag 'set_locale', options_for_select(LANGUAGES, I18n.locale.to_s), onchange: 'this.form.submit()' %>
<%= submit_tag 'submit' %>
<%= javascript_tag "$('.locale input').hide()" %>
<% end %>
<%= image_tag("logo.png") %>
<%= #page_title || t('.title') %>
</div>
the routing folder is as follows:
scope'(:locale)' do
resources :users
resources :orders
resources :line_items
resources :carts
resources :products do
get :who_bought, on: :member
end
root to: 'store#index', as: 'store'
end
Cant figure out what I did wrong. If I enter /en or /es in the url it works correctly. However choosing it in the drop down that is created I get the error mentioned
Found the issue, the form_tag was expecting a POST so I changed
<%= form_tag store_path, class: 'locale' do %>
to
<%= form_tag store_path, class: 'locale', :method => :get do %>
and it worked
You are missing an slash in the scope as guides states:
# config/routes.rb
scope "/:locale" do
resources :books
end
http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params