Show User name instead of id in Rails partial view - ruby-on-rails

I know there must be a very simple solution, but I just can't figure out how.
Inside the auction show view, I am displaying a list off all bids with the regarding bidder (=User who have placed the bid). When placing a new bid, the user_id is successfully saved within the bid table.
I want to display the bidders name instead of his ID. (with #bidder = User.find(#bid.user_id))
bids_controller.rb:
class BidsController < ApplicationController
# Create new bid
def create
# find Auction related to new Bid
#auction = Auction.find(params[:auction_id])
#bid = #auction.bids.create(bid_params)
#bid.user_id = current_user.id
# Find the bidder within User Table
#bidder = User.find(#bid.user_id)
#bid.save
redirect_to auction_path(#auction), :notice => "Bid placed"
end
Bids Partial view: views\bids\ _bid.html.erb
<p>
<strong>Bidder:</strong>
<%= #bidder.name %>
</p>
<p>
<strong>Bid-Amount:</strong>
<%= bid.amount %>
</p>
<p>
<%= link_to 'Destroy Bid', [bid.auction, bid],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
The whole thing is displayed within the Auctions Show View:
views\auctions\show.html.erb
<h2>Bids</h2>
<%= render #auction.bids %>
<h2>Place a Bid:</h2>
<%= render 'bids/form' %>
<h2>Comments</h2>
<%= render #auction.comments %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
The Error:
NoMethodError in Auctions#show Showing
C:/Users/santa/Documents/rails/app/views/bids/_bid.html.erb
where line #3 raised:
undefined method `name' for nil:NilClass Extracted source (around line
3): 1 2 3 4 5 6
<p>
<strong>Bidder:</strong>
<%= #bidder.name %> </p>
<p>
Trace of template inclusion: app/views/auctions/show.html.erb
name exists in Users table

Instead of <%= #bidder.name %> try add <%= bid.user.name %>. If user_id saving properly in bid table then clearly there is has_many and belongs_to association there.
Just replace <%= #bidder.name %> to <%= bid.user.name %>

Related

Wrong action called when using Partial

I’m trying to use Partial to render a form into Edit and Create view (to handle both actions). The problem is that it works with Create action but the form does not work for editing, it only creates. It seems that the app ignore and use create action by default (the submit button is automatically named “Create Expense” instead of “Update Expense”).
By using this kind of route, I want to edit an Expense (that is linked with a Category).
I did the navigation from Category show view to my Expense edit form view like this.
<% #category.expenses.each do |expense| %>
<tr>
<td><%= expense.name %></td>
<td>
<%= link_to "Edit", edit_category_expense_path(#category, expense) %>
</td>
</tr>
<% end %>
I’m using Partial for rendering my form into Expense edit view.
<h1>Edit Expenses</h1>
<%= render "form", expense: #expense %>
Then my form is simply like this.
<%= form_with model: [ #category, #expense] do |form| %>
<p>
<%= form.label :name %><br>
<%= form.text_field :name %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
My Expense controller
def edit
#expense = Expense.find(params[:id])
#category = #expense.category
end
def new
#expense = #category.expenses.new
end
Finally, i'm rendering the creation form in my Category show view.
<h2>Add Expense:</h2>
<%= render 'expenses/form' %>
But I got undefined method 'model_name' for nil:NilClass when I'm trying to access to my Category show view
Solution based on #Chiperific answer
According to my project structure, I had to put #expense = Expense.new in the show action of Category controller. That's where I displayed the form.
As for the edit form, the answer of #Chiperific explains it perfectly
Thanks
.build instantiates a new record. Rails recognizes that the record is new, not persisted, and automatically does what you are seeing (send the form data to the #new action and use 'Create' for the button language.
In your controller, you need to get the existing record you want to edit:
...
def new
#expense = #category.expenses.new
end
def edit
#expense = Expense.find(params[:id])
end
And adjust your form:
<%= form_with model: [ #category, expense ] do |form| %>
...

Rails:ArgumentError in MethodTypes#edit

I tried to make an edit for ruby on rails, but it shows me the argument error about the edit. I am confused about this question.
Then, I have tried to put the different argument into index.html.erb However, it still does not work. For example m.id and m
This is index.html.erb
<% #methodtypes.each do|m| %>
<tr>
<td><%=m.name %></td>
<td><%=m.desp %></td>
</tr>
<%= link_to "Edit", edit_method_types_path(m.id) %>
<% end %>
<%= link_to "Create Method", new_method_types_path %>
This is my controller file:
class MethodTypesController < ApplicationController
def index
#methodtypes = MethodType.all
end
def show
#methodtype = MethodType.find_by_id(params[:id])
end
def create
#methodtype = MethodType.new(method_params)
#methodtype.save
if #methodtype.save
redirect_to method_types_path
else
render :new
end
end
def edit
#methodtype = MethodType.find_by_id(params[:id])
end
def new
#methodtype = MethodType.new
end
private
def method_params
params.require(:method_type).permit(:name, :desp)
end
This is my edit page which is edit.html.erb:
<%= form_for #methodtype do |f| %>
<div>
<%= f.label :name %>
<%= f.text_area :name %>
</div>
<div>
<%= f.label :desp %>
<%= f.text_field :desp %>
</div>
<%= f.submit %>
<% end %>
The result should show that I can edit my text. but, it shows the ArgumentError in MethodTypes#edit. Does someone can give me some suggestion, I do not know how to fix that.....
Wrong edit url path
It should be <%= link_to "Edit", edit_method_type_path(m.id) %> instead of <%= link_to "Edit", edit_method_types_path(m.id) %>
Also check your routes file It seems you are defining
resource: method_types
Change to
resources: method_types
<%= link_to "Edit", edit_method_types_path(m.id) %> should be <%= link_to "Edit", edit_method_type_path(m) %>, note that type is in singular.
Run rails routes -g method_type to confirm it.
Also, change the MethodType.find_by_id(params[:id]) to MethodType.find(params[:id]) in the controller.
Btw, you are calling save twice in your create method:
def create
#methodtype = MethodType.new(method_params)
#methodtype.save # delete this line
if #methodtype.save
redirect_to method_types_path
else
render :new
end
end

ruby on rails project pulling from an api trying to save bids from bids controller and display on the user show page

I'm accessing an api to pull the lowest price and event id and then based on the current lowest price you can place a bid I'm currently stuck at just creating a bid and saving it to the user show page I can manually enter the params on the event show page which triggers my create method on bid page which then all bids placed by a user should show up on a user show page because a user has many bids and bid belongs to a user below is a snippet from my event show page showing the ability to manually create a bid and save to the database
<%= form_tag "/bids", method: :post do %>
<div>
<%= label_tag :bid, "place your bid " %>
<%= text_field_tag :bid %>
</div>
<div>
<%= label_tag :event %>
<%= text_field_tag :event_id, params[:id] %>
</div>
<div>
<%= label_tag :user_id %>
<%= text_field_tag :user_id %>
</div>
<div>
<%= label_tag :lowest_price %>
<%= text_field_tag :lowest_price %>
</div>
<%= submit_tag "Submit" %>
<% end %>
here is my create method in my bids controller for the time being
def create
#bidd = Bid.new(event_id: params[:event_id], user_id: params[:user_id], bid: params[:bid], lowest_price: params[:lowest_price])
if session[:user_id] == current_user.id
#bidd.save
flash[:success] = "bid created."
redirect_to "/users/#{current_user.id}"
else
flash[:warning] = 'please sign in'
redirect_to '/login'
end
end
and then here is my user/show page which is just #bidd in skull tags i know i need something like at #bidd.bid but im creating from my bid controller and showing on the user page so basically I'd like to find a way to display all the users bids on a user show page and also not have to manually enter the event id and current lowest price and user id to save a bid. also the the event id is being pulled from an api i thought first to make a userbid thru model but didnt think that would be necessary
#<%maybe something like # <% #bidd.each do |a| %>
<p>event: <%= a.event_id %></p>
<p>Price: <%= a.bid %></p>
<% end %>
i had a naming issue in my user show method
def show
#bids = Bid.where(user_id: current_user.id)
render 'show.html.erb'
should be
#bidds
and
<% #bidds.each do |a| %>
<p>event: <%= a.event_id %></p>
<p>Price: <%= a.bid %></p>
<% end %>
worked to display that users bids now lets see if it can be done without having to fill in those params manually on the event show page.

RoR Rnder a partial cant find method

I have a classes User and Company, I want to re-use the users partial as the to render company staff.
In my CompaniesController I have:
def staff
#company=Company.find(params[:id])
#users=#company.works_fors.paginate(page: params[:page], :per_page => 10)
#title=#company.name+" staff."
end
And in my staff.html.erb template I have:
<% if #users.any? %>
<ul class="users follow">
<%= render #users %>
</ul>
<%= will_paginate %>
<% end %>
This is the works_fors/_works_for partial:
<%= render :partial => 'user' %>
Which Renders
<li>
<%= gravatar_for user, size: 50 %>
<%= link_to user.name, user %>
<% if current_user.developer? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</li>
However this throws an error on the user object as it cant find the method
undefined local variable or method `user' for~~
I think this is because Im calling the user object from within companies but there is a defined relationship, or do I need to redefine in companies ?
It's hard to tell, but it appears that what you call #users in your controller is in fact not a User collection, but a WorkFor collection.
#users = #company.works_fors...
What you mean is:
#works_fors = #company.works_fors...
This means that staff.html.erb is working with a works_for collection. So you should rename the variable in your template to avoid confusion.
# staff.html.erb
<% if #works_fors.any? %>
<ul class="users follow">
<%= render #works_fors %>
</ul>
<%= will_paginate #works_fors %>
<% end %>
Now we know we are rendering a works_for partial. So an instance of works_for is be available inside the partial. We need to ask it for its associated user instance, and pass it to the render method.
# works_fors/_works_for.html.erb
<%= render works_for.user %>
As a bonus, you can save yourself some queries by preloading the users.
#works_fors = #company.works_fors.includes(:user)...

How to hide form when there is one instance of the associated model #rails #beginner

I'm trying to create a comment like system where the user can add a 'outcome' to a 'decision'. I like to hide the outcome create form to 'hide' when there is 1 added to the decision.
My best guess to accomplish this, is to count the number of associates instances of outcomes to decisions and create a if statement for hiding the form when there is more than 0 instances of outcome.
I tried different ways of accomplishing this but i can't seem to make the counting or if statement to work. I am new to coding for your reference :-).
Can anyone give me a suggestion on how to tackle this?
My code:
controllers/decisions_controller.rb
class DecisionsController < ApplicationController
before_action :find_decision, only: [:show, :edit, :update, :destroy]
def index
# gets all rows from decision table and puts it in #decision variable
#decisions = Decision.all
end
def show
# find only the decision entry that has the id defined in params[:id]
end
# shows the form for creating a entry
def new
#decision = Decision.new
end
# creates the entry
def create
#decision = Decision.new(decision_params)
if #decision.save
redirect_to #decision
else
render 'new'
end
end
# shows the form for editing a entry
def edit
end
# updates the entry
def update
if #decision.update(decision_params)
redirect_to #decision
else
render 'edit'
end
end
def destroy
#decision.destroy
redirect_to root_path
end
private
def find_decision
# Loads the right entry.
#decision = Decision.find(params["id"])
end
def decision_params
params.require(:decision).permit(:title, :forecast, :review_date)
end
end
controllers/outcomes_controller.rb
class OutcomesController < ApplicationController
def create
#decision = Decision.find(params[:decision_id])
#outcome = #decision.outcomes.create(params[:outcome].permit(:actual, :strength, :weakness))
redirect_to decision_path(#decision)
end
end
models/outcome.rb
class Outcome < ActiveRecord::Base
belongs_to :decision
end
models/decision.rb
class Decision < ActiveRecord::Base
has_many :outcomes
end
decisions/show.html.erb
<h1>Decision showpage</h1>
<h2><%= #decision.title %></h2>
<p><%= #decision.created_at %></p>
<p><%= #decision.forecast %></p>
<p><%= #decision.review_date %></p>
<%= render #decision.outcomes %>
<%= link_to "Delete Decision", decision_path(#decision), method: :delete, data: { confirm: "Are you sure?" } %>
<%= render "outcomes/form" %>
<%= render "outcomes/outcome" %>
outcomes/_form.html.erb
<%= form_for([#decision, #decision.outcomes.build]) do |f| %>
<%= f.label :actual %>:
<%= f.text_field :actual %> <br/>
<%= f.label :strength %>:
<%= f.text_area :strength %> <br/>
<%= f.label :weakness %>:
<%= f.text_area :weakness %> <br/>
<%= f.submit %>
<% end %>
outcomes/_outcome.html.erb
<h2>outcome</h2>
<%= #decision.outcomes.first.actual %> </br>
<h3>What i found i'm good at</h3>
<%= #decision.outcomes.first.strength %> </br>
<h3>What i found i'm weak at</h3>
<%= #decision.outcomes.first.weakness %>
<h1>Decision showpage</h1>
<h2><%= #decision.title %></h2>
<p><%= #decision.created_at %></p>
<p><%= #decision.forecast %></p>
<p><%= #decision.review_date %></p>
<%= render #decision.outcomes %>
<%= link_to "Delete Decision", decision_path(#decision), method: :delete, data: { confirm: "Are you sure?" } %>
<%if #decision.outcomes.length < 1 %>
<%= render "outcomes/form" %>
<%end%>
<%= render "outcomes/outcome" %>
Sounds like standard conditional logic:
#app/views/decisions/show.html.erb
<h1>Decision</h1>
<h2><%= #decision.title %></h2>
<% %w(created_at forecast review_date).each do |option| %>
<%= content_tag :p, #decision.send(option) %>
<% end %>
<% if #decision.outcomes.any? %>
<%= render #decision.outcomes %>
<%= render "outcomes/form" %>
<% end %>
<%= link_to "Delete Decision", decision_path(#decision), method: :delete, data: { confirm: "Are you sure?" } %>
My best guess to accomplish this, is to count the number of associates instances of outcomes to decisions and create an if statement for hiding the form when there is more than 0 instances of outcome.
Sure - it's called conditional logic
In your case, you're looking for several potential methods (to determine the size of the associated data):
length (Pardeep's answer) -- queries the array's length
size -- either invokes count (SQL query) or length
count -- performs db query (should only be used if no data loaded)
any? -- very similar to length except returns true/false depending on whether any items exist
Good ref here: ActiveRecord: size vs count
if you already load all entries, say User.all, then you should use
length to avoid another db query
if you haven't anything loaded, use count to make a count query on
your db
if you don't want to bother with these considerations, use size which
will adapt
I used .any? because it's the most succinct in this instance.

Resources