hi im having a bit trouble in my application im new in ROR development
i made a static page where my function rooms of my reservation
application are showed and can be add in in the
reservation_function_room(line item of reservation functionroom) it
raises uninitialized constant Reservations when ever i route to <%= link_to "add functionrooms", reservation_pages_functionroom_path(#reservation) %> cant figure out whats wrong
very thanks in advance thanks
page-static pages
class PagesController < ApplicationController
def functionroom
#reservation = Reservation.find(params[:reservation_id])
#function_room = FunctionRoom.all
end
end
functionroom.html.erb
<% if notice %>
<p id = "notice"><%= notice%></p>
<%end%>
<h1>functionRooms</h1>
<%#function_room.each do |functionroom|%>
<h3><%= functionroom.name%></h3>
<p><%= number_to_currency(functionroom.price)%></p>
<%= button_to 'add function room',
reservation_reservation_function_room_path(:function_room_id =>
functionroom), :method => :post,:remote => true%>
<%end%>
reservation_contoller.rb
def index
#reservations = Reservation.all
end
def show
#reservation = Reservation.includes(:reservation_function_rooms =>
:function_room,:reservation_package => :package).find(params[:id])
end
class ReservationFunctionRoomsController < InheritedResources::Base
def show
#reservation_function_room =
ReservationFunctionRoom.find(params[:id])
end
def new
#reservation_function_room = ReservationFunctionRoom.new
end
def create
#reservation = Reservation.find(params[:reservation_id])
function_room = FunctionRoom.find(params[:function_room_id])
#reservation_function_room =
#reservation.add_function_room(function_room.id)
if #reservation_function_room.save
redirect_to #reservation, :notice => "function room successfuly
added"
end
end
end
routes
get "pages/menu"
resources :reservation_function_rooms
resources :services
resources :reservations do
get "pages/functionroom"
end
resources :reservation_packages
resources :package_line_items
resources :packages do
resources :package_crews
end
resources :function_rooms
reservation.rb
class Reservation < ActiveRecord::Base
has_one :reservation_package
belongs_to :service
has_many :reservation_function_rooms
has_many :package_line_items
has_many :menus , :through => :package_line_items, :uniq => true
has_many :function_rooms, :through =>:reservation_function_rooms
def add_function_room(function_room_id)
current_function_room =
reservation_function_rooms.find_by_function_room_id(function_room_id)
if current_function_room
redirect_to #reservation, :notice => "function room already
added"
else
current_function_room =
reservation_function_rooms.build(:function_room => function_room_id)
current_function_room.price =
current_function_room.function_room.price
end
current_function_room
end
end
reservation/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #reservation.name %>
</p>
<%= display_package #reservation%>
<p>
<b>Address:</b>
<%= #reservation.address %>
</p>
<p>
<b>Contact:</b>
<%= #reservation.contact %>
</p>
<p>
<b>Date:</b>
<%= #reservation.date %>
</p>
<p>
<b>Timestart:</b>
<%= #reservation.timeStart %>
</p>
<p>
<b>Timeend:</b>
<%= #reservation.timeEnd %>
</p>
<p>
<b>Numguest:</b>
<%= #reservation.numGuest %>
</p>
<p>
<%= link_to "add functionrooms", reservation_pages_functionroom_path(#reservation) %>
</p>
<table>
<tr>
<th>Function room</th>
<th>Price</th>
</tr>
<% #reservation.reservation_function_rooms.each do |room|%>
<tr>
<td><%= room.function_room.name%></td>
<td><%= room.function_room.price%></td>
</tr>
<%end%>
</table>
<%= link_to 'Edit', edit_reservation_path(#reservation) %> |
<%= link_to 'Back', reservations_path %>
if anything is needed feel free to ask thanks in advance :)
I know it's been 4 years, but I ran through the same issue and I think I may have found where it comes from.
You have dependency "has_one :reservation_package"
and lower you have "resources :reservation_packages"
I think the "s" here is the problem : either do a single resource OR a has_many relation. That why he can't find it.
That's how i fixed my problem.
Related
This app has the following models:
Farm (has_many :crops)
Crop (belongs_to :farm, has_many :issues)
Issue (belongs_to :crop)
Here are the routes:
resources :farms do
resources :crops do
resources :issues
end
end
I want a user to be able to create a new "issue" from the Farm#show page that lists all the farm's crops. Here is the form that is causing the error on the Farm#show page:
undefined method `crop_issues_path' for #<#:0x007fa814a3cc30>
#from the show action on the controller:
##farm = Farm.find(params[:id])
##crops = #farm.crops
<% #crops.each do |crop| %>
<%= crop.id %>
<%= form_for([crop, crop.issues.build]) do |f| %>
<%= f.select(:issue_type, options_for_select([['mold'], ['pests'], ['dehydration'], ['other']])) %>
<%= f.text_area :notes %><br>
<%= f.submit "New Issue", :class => "button" %>
<% end %>
<% end %>
My create action on issues controller:
def create
#crop = Crop.find(params[:crop_id])
#issues = #crop.issues.create(params[:issue].permit(:issue_type, :notes, :crop_id))
redirect_to :back
end
I have used nearly identical code when the crops and issues were not nested under farms, and it works. I believe the issue is because of the nesting, but cannot figure out a solution.
I think your problem is with the object you're binging the form to. It should be #farm, as you're in the #farms show action.
I modified it to this:
<% #crops.each do |crop| %>
<%= crop.id %>
<%= form_for([#farm, crop, crop.issues.build]) do |f| %>
<%= f.text_area :notes %><br>
<%= f.submit "New Issue", :class => "button" %>
<% end %>
<% end %>
with my controller like this:
class FarmsController < ApplicationController
def index
end
def show
#farm = Farm.find_by_id(params[:id])
#crops = #farm.try(:crops)
end
end
Currently I'm looping to show each post and inside that loop looping to show all related replies. However currently all replies are showing up under every post on every board.
How can I only show the replies that are related to each post?
resources :boards, :path => '' do
resources :posts, :path => 'thread' do
resources :replies
class BoardsController < ApplicationController
def show
#board = Board.friendly.find(params[:id])
#boards = Board.all
#replies = Reply.all
end
class PostsController < ApplicationController
def show
#board = Board.friendly.find(params[:board_id])
#boards = Board.all
#replies = Reply.all
#post = Post.includes(:board).where('boards.slug' => params[:board_id]).friendly.find(params[:id])
end
class Board < ActiveRecord::Base
has_many :posts
has_many :replies, through: :posts
include FriendlyId
friendly_id :name, use: :slugged
accepts_nested_attributes_for :posts, :replies
end
class Post < ActiveRecord::Base
belongs_to :board
has_many :replies, dependent: :destroy
accepts_nested_attributes_for :replies
include FriendlyId
friendly_id :pid, :use => :scoped, :scope => :id
end
views/boards/show:
<% #board.posts.find_each do |post| %>
<%= post.subject %>
<%= post.name %>
<%= post.email %>
<%= post.created_at %>
No.<%= post.pid %>
<%= link_to "[reply]", board_posts_path(#board, #post)%>
<br>
<%= post.comment %><br><br>
<% render "replies/replies" %>
<% end %>
views/replies/_replies:
<% #replies.each do |reply| %>
<p>
>><%= reply.name %>
<% reply.created_at %>
<%= reply.email %>
<%= reply.subject %>
<%= reply.created_at.strftime("%m/%d/%Y(%a) %I:%M:%S ") %>
No.<%= reply.pid %>
<br>
<%= reply.comment %>
</p>
<% end %>
you don't need to set replies at controller level as replies belongs to posts. you can get all the replies for a given post by post.replies
in you view
#posts.each do |post|
replies = post.replies // get all the replies that belongs to this post
replies.each do |reply|
reply.attribute
end
end
Since you want to show only related replies in _replies, do not use generic #replies, use post there:
views/boards/show:
...
<% render "replies/replies", locals: {post: post} %>
views/replies/_replies:
<% post.replies.each do |reply| %>
...
I have one model "Breads" that has_many "Posts".
I would like to have a form to create a new "Post" on the 'show' page for a given "Bread" that creates the association to the record of 'Bread' which the 'show' page is displaying.
I have tried a few different methods, but all are giving an error. The method that I have shown below gives a "Association cannot be used in forms not associated with an object" error.
/views/breads/show.html.erb:
<p>
<strong>Bread Type:</strong>
<%= #bread.bread_type %>
</p>
<table>
<tr>
<th>Uploaded By</th>
<th>Comment</th>
<th>Picture</th>
</tr>
<% #bread.posts.each do |post| %>
<tr>
<td><%= post.uploader %></td>
<td><%= post.comment %></td>
<td><%= image_tag post.attachment_url.to_s %></td>
</tr>
<% end %>
</table>
<%= #bread.id %>
<%= simple_form_for #bread do |b| %>
<%= simple_fields_for :posts do |p| %>
<%= p.input :uploader %>
<%= p.input :comment %>
<%= p.association :bread, value: #bread.id %>
<%= p.file_field :attachment %><br>
<%= p.button :submit %>
<% end %>
<% end %>
<%= link_to 'Back', breads_path %>
config/routes.rb
Rails.application.routes.draw do
get 'welcome/index'
root 'welcome#index'
resources :breads
resources :posts
end
controllers/breads_controller.rb:
class BreadsController < ApplicationController
def index
#breads = Bread.all
end
def show
#bread = Bread.find(params[:id])
end
def new
#bread = Bread.new
end
def edit
#bread = Bread.find(params[:id])
end
def create
#bread = Bread.new(bread_params)
if #bread.save
redirect_to #bread
else
render 'new'
end
end
def update
#bread = Bread.find(params[:id])
if #bread.update(bread_params)
redirect_to #bread
else
render 'edit'
end
end
def destroy
#bread = Bread.find(params[:id])
#bread.destroy
redirect_to breads_path
end
private
def bread_params
params.require(:bread).permit(:bread_type)
end
end
models/bread.rb:
class Bread < ActiveRecord::Base
has_many :posts
validates :bread_type, presence: true, uniqueness: true
end
models/post.rb:
class Post < ActiveRecord::Base
belongs_to :bread
mount_uploader :attachment, AttachmentUploader
end
Do this -
<%= simple_form_for #bread do |b| %>
<%= b.simple_fields_for(:posts,#bread.posts.build) do |p| %>
<%= p.input :uploader %>
<%= p.input :comment %>
<%= p.file_field :attachment %><br>
<%= p.button :submit %>
<% end %>
<% end %>
and make changes in beard_params
def beard_params
params.require(:bread).permit!
end
Here permit! requires all parameters and for other way you can use #pawan's answer.
Extending #Amit Suroliya answer, you need to add posts_attributes to bread_params
def bread_params
params.require(:bread).permit(:id, :bread_type, posts_attributes: [:id, :uploader, :comment, :bread_id, :attachment])
end
Update:
You also need to add accepts_nested_attributes_for :posts in Bread model.
Iam sorry, but this is not good way at all, try to don't abuse rails and rest routes :)
Here is easy example how to do that:
config/routes.rb
resources :bread do
resources :posts
end
This means there will be routes like:
bin/rake routes
breads - breads#index
bread/:id - breads#show
etc..
and most important
bread/:bread_id/posts/:id
...
That means posts are nested resources for bread...
app/controllers/breads_controller.rb
controller BreadsController < BaseController
before_action :find_bread, except: %i(index create new)
.... action new, update, edit etc..
end
but now its the important part in PostsController..
app/controllers/posts_controller.rb
controller PostsController < BaseController
before_action :find_bread
before_action :find_post, except: %i(index new create)
before_action :build_post, only: %i(new create)
.... action new, update, edit etc..
# Example with :return link
def create
if #post.save
if params[:back] == 'bread_show'
redirect_to bread_path(#bread)
else
redirect_to bread_post_path(#bread, #post)
end
else
render 'new'
end
end
private
def build_post
if params[:post]
#post = #bread.posts.build(post_params)
else
#post = #bread.posts.build
end
end
def find_post
#post = #bread.posts.find(params[:id])
end
def find_bread
#bread = Bread.find(params[:bread_id])
end
... post params ...
end
Now you have rest full routes and you're able to do what you want without such a pain and clean
... output hidden
<%= #bread.id %>
<%= simple_form_for #bread.posts.build do |b| %>
<%= p.input :uploader %>
<%= p.input :comment %>
<%= p.file_field :attachment %><br>
<%# Send back link to return on proper page %>
<%= p.hidden_field :back, 'bread_show' %>
<%= p.button :submit %>
<% end %>
<%= link_to 'Back', breads_path %>
There can be some mistakes, I write this code from memory, can't try that :(
I'm getting a curious error after submitting my form. Been trying to solve this for several hours..
No route matches {:action=>"show", :controller=>"items", :item_id=>"141", :matter_id=>"3"} missing required keys: [:id]
The parameters are:
{"utf8"=>"✓",
"authenticity_token"=>"w0D7XmX2X2/ZMU19T6RlMvWCEClXnCFFOR+4EdIFvWg=",
"comment_item"=>{"item_id"=>"",
"name"=>"kaljdf",
"body"=>"yet another comment test"},
"commit"=>"Post Comment",
"matter_id"=>"3",
"item_id"=>"141"}
I have the following models:
class Matter < ActiveRecord::Base
has many :discoveries
delegate :items, to: :discoveries
end
class Discovery < ActiveRecord::Base
belongs_to :matter
scope :items, -> { where(type: 'Item') }
end
class Item < Discovery
has_many :comment_items
end
class CommentItem < ActiveRecord::Base
belongs_to :item
end
Controllers:
class ItemsController < DiscoveriesController
def show
#item = Item.find(params[:id])
#comment_item = CommentItem.new
end
def edit
#item = Item.find(params[:id])
end
def new
#item = Item.new
end
end
class CommentItemsController < ApplicationController
before_action :set_comment_item, only: [:show, :edit, :update, :destroy]
def new
#item = Item.find(params[:item_id])
#comment_item = #item.comment_item.new
end
def create
#item = Item.find(params[:item_id])
#comment_item = #item.comment_items.new(comment_item_params)
if #comment_item.save
flash[:notice] = 'Comment was successfully created'
redirect_to matter_item_url(matter_id: params[:matter_id])
else
flash[:notice] = "Error creating comment: #{#comment.errors}"
redirect_to matter_item_url(#matter, #item)
end
end
def destroy
#comment_item = CommentItem.find(params[:id])
#comment_item.destroy
redirect_to(#comment_item.item)
end
private
def set_comment_item
#comment_item = CommentItem.find(params[:id])
end
def comment_item_params
params.require(:comment_item).permit(:name, :body, :item_id, :matter_id)
end
end
The show action for the item resource:
<p>
<strong>Matter:</strong>
<%= #item.matter_id %>
</p>
<p>
<strong>Content:</strong>
<%= #item.content %>
</p>
<hr />
<%= form_for #comment_item, url: matter_item_comment_items_path(matter_id: #item.matter, item_id: #item.id) do |f| %>
<% if #comment_item.errors.any? %>
<ul>
<% #comment_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<%= f.hidden_field :item_id %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Post Comment" %>
</p>
<% end %>
<%= render :partial => 'comment_item', :collection => #item.comment_items %>
<%= link_to 'Edit', edit_matter_item_path(id: #item.id) %> |
<%= link_to 'Back', matter_items_path %>
Routes
resources :items do
resources :comment_items
end
resources :matters do
resources :items do
resources :comment_items
end
end
When looking at CommentItems in the console, I see that the comments are in fact being added to the model with their correct ID's, but they don't seem to be passed as parameters.. What am I missing?
I've reviewed Rails 4 form_for double nested comments and Rails 3.2 - Nested Resource Passing ID but I didn't have much luck..
I'd really appreciate your help!
No route matches {:action=>"show", :controller=>"items", :item_id=>"141", :matter_id=>"3"} missing required keys: [:id]
your request is going to ItemsController instead of CommentItemsController
see :controller => "items"
I'm new to Rails and I'm building a quizz application.
I have a has_many and belongs_to association set up between two models: Level and Question.
#models/level.rb
class Level < ActiveRecord::Base
has_many :questions
end
#models/question.rb
class Question < ActiveRecord::Base
belongs_to :level
attr_accessible :level_id
end
I have an action 'startlevel' in my LevelController that simply lists all the levels.
class LevelsController < ApplicationController
def startlevel
#levels = Level.all
end
and the view with links to go to the first question of the level. I want to add the id of the level as a parameter in the link. I noticed the 1 in the url of the view. I have no idea why how it came there and if it is part of my problem.
#controller/levels/startlevel/1
<h2>which level would you like to play</h2>
<table>
<tr>
<th>level</th>
</tr>
<% #levels.each do |level| %>
<tr>
<td> level <%=level.number %></td>
<td><%= link_to '<> play this level', :controller => "questions", :action => "answer",:level_id=> level.id%></td>
</tr>
<% end %>
</table>
When I follow the the link, I want to go to the first question with a level_id that matches the id parameter in the link_to, so i tried to do this:
class QuestionsController < ApplicationController
def answer
#question = Question.find_by_level_id(params[:level_id])
end
with this view
<p>
<b>Question:</b>
<%=h #question.word %>
</p>
<p>
<b>1:</b>
<%=h #question.ans1 %>
</p>
<p>
<b>2:</b>
<%=h #question.ans2 %>
</p>
<p>
<b>3:</b>
<%=h #question.ans3 %>
</p>
<p>
<b>4:</b>
<%=h #question.ans4 %>
</p>
<%= form_tag(:action => "check", :id => #question.id) do %>
<p>
<b>the correct answer is number: </b>
<%=text_field :ans, params[:ans]%>
</p>
<p><%= submit_tag("check")%></P>
<% end %>
unfortunately, whatever i tried, all i got for the last couple of hours was:
undefined method `word' for nil:NilClass
(word is an attribute of a question)
I want to cry. what am I doing wrong?
p.s. my idea is to add a link_to_unless in the 'answer'view which goes to the next question of the same level unless the next one is nil, so i think i need to group the questions with the same reference key somehow?
It works right now, however i'm not sure if this is the prettiest solution. Views/levels/play is empty since it only redirects to the first question of the level.
class LevelsController < ApplicationController
def startlevel
#levels = Level.all
end
def play
#level = Level.find(params[:id])
#question = Question.find_by_level_id(params[:id])
redirect_to controller: 'questions', action: 'answer', id: #question.id
end
#views/levels/startlevel
<h2>Which level would you like to play</h2>
<table>
<tr>
<th>level</th>
</tr>
<% #levels.each do |level| %>
<tr>
<td> level <%=level.number %></td>
<td>
<%= link_to '<> Play this level', :action => "play", :id=>level.id %></td>
</tr>
<% end %>
</table>
QuestionsController
class QuestionsController < ApplicationController
def answer
#question= Question.find(params[:id])
end
edit: Routes:
quizz::Application.routes.draw do
resources :questions
resources :levels
get "home/index"
root :to => 'home#index'
match ':controller/:action/:id', via: [:get, :post]
match ':controller/:action/:id.:format', via: [:get, :post]