I have added a custom action in my controller called transplant. I simply want to render a dropdown form to select where to be located based on the 'tray_id'
my routes look like this:
resources :plants do
member do
get 'transplant'
end
resources :plantdats, :plant_cycles, :tasks
end
My controller looks like this:
before_action :set_plant, only: [:show, :edit, :update, :destroy, :transplant]
def transplant
if #plant.update(plant_params)
redirect_to #plant
flash[:success] = "Transplanted successfully"
end
end
def set_plant
#plant = Plant.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def plant_params
params.require(:plant).permit(:title, :notes, :category_id, :tray_id, images_files: [])
end
Here is my button calling the action
<%= link_to 'TRANSPLANT', transplant_plant_path(#plant), class: "btn btn-raised btn-info hoverable" %>
Here is my transplant page _transplant.html.erb
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="jumbotron">
<%= form_for(#plant, html: {class: 'form-horizontal'}) do |f| %>
<% if #plant.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#plant.errors.count, "error") %> prohibited this grow from being saved:</h2>
<ul>
<% #plant.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label 'NAME' %>
<%= f.hidden_field :tray_id, value: params[:tray_id] %>
<% if params[:tray_id].nil? %>
<%= f.collection_select(:tray_id, Tray.all, :id, :title) %></br></br>
<% end %>
<%= f.submit class: 'btn btn-raised hoverable btn-success' %>
<% end %>
</div>
</div>
</div>
EDIT
After implementing the route to post 'transplant
and changing my link code to
<%= link_to "TRANSPLANT", transplant_plant_path(#plant, tray_id: #plant.tray_id), method: "post", class: "btn btn-raised hoverable" %>
I still get the same error. It points right to the plant_params code in my controller.
These are the params that are being passed:
{"_method"=>"post",
"authenticity_token"=>"fhSKt2DpgTwt1J4HsoBqYFSs0B9+pgSvDDxrS/u6yo4c3gvSxYlrrFDmhbPXq+cMho/eTHY+194WZ8zpcb1txA==",
"id"=>"1",
"format"=>"1"}
Im simply trying to update the :tray_id
Ive been at this all day, can anyone help with the error that Im getting?
You should probably provide your code for your transplant action and view. Based on what you provided, it seems like you're trying to build a link, which changes the tray of a plant when clicked. If that's the case, transplant should probably be a POST route instead of GET. Also, you probably want to provide the tray_id in your post link like this:
<%= link_to "TRANSPLANT", transplant_plant_path(#plant, tray_id: {{your id}}), method: "post", class: "..." %>
Then you can get tray_id in your transplant through params[:tray_id] and re-associate your plant and tray
Essentially what I was trying to do was not easily done and my approach needed to change. I simply rendered the transplant form in my view and it works fine now. Thanks again :)
Check your routes you just have defined get route and your request is post after the form submission
Related
am having an issue related to ActiveRecord and am totally stuck in this.
basically am trying to call a controller method from a partial html.erb file. it's passing the username in id when I call it from partial and correct id when I call it from it's original HTML file.
wallets_controller:
before_action :find_wallet, only: %i[update]
def update
//some code ...
end
private
def find_wallet
// issue is here...
#wallet = Wallet.find(params[:id])
end
update.html.erb:
<%= f.fields_for :wallets do |wallet| %>
<%= render 'wallet_fields', f: wallet %>
<% end %>
_wallet_fields.html.erb:
<div class="flex-shrink-1">
<%= link_to update_nfts_wallet_path(wallet: f.object), remote: true, method: :post, class: 'btn btn-icon nfts-refresh-btn' do %>
<span class="material-icons-outlined">refresh</span>
<% end %>
</div>
it's throwing this error: ActiveRecord::RecordNotFound (Couldn't find Wallet with 'id'=hamid)
when add my Link_to update_nfts_wallet_path(wallet: f.object) from _wallet_fields.html.erb
and in controller I get actionController like this
and if I add this code in update.html.erb
<%= f.fields_for :wallets do |wallet| %>
<div class="flex-shrink-1">
<%= link_to update_nfts_wallet_path(wallet), remote: true, method: :post, class: 'btn btn-icon nfts-refresh-btn' do %>
<span class="material-icons-outlined">refresh</span>
<% end %>
</div>
<% end %>
then in controller, I get ActionController like this:
I need it the same as this when I call it from partial.
passing the same wallet from partial then why there is different parameters in ActionController. it's not finding the wallet because id have username not an id.
Change
update_nfts_wallet_path(wallet: f.object)
to
update_nfts_wallet_path(id: f.id)
I created a button where users can input stuff in a field and then press the button to update the database (put request) which can be seen here in show.html.erb:
<% provide(:title, #user.name) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
<br>
<%= #user.email %>
<% if #errors %>
<p>THE FORM COULD NOT BE SAVED </p>
<ul id='errors'>
<% #errors.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
<% end %>
<br>
<% if is_admin? %>
<% if !#user.admin %>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<%= f.label :wistia_project_id %>
<%= f.text_field :wistia_project_id, class: 'form-control' %>
<%= f.submit "Save", :action => "set_wistia_project_ID", :method => :patch, :form_class => "form-control" %>
<% end %>
</div>
</div>
<% end %>
<% end %>
</h1>
</section>
</aside>
</div>
The function is in user_controller.rb:
# Sets wistia_project_ID.
def set_wistia_project_ID
#user = User.find(params[:id])
#user.set_project_id
unless #user.valid?
#errors = #user.errors.full_messages
render :show
end
end
That function calls another function, just to separate things more clearly. This other function lives in user.rb:
# Sets the wistia_project_ID.
def set_project_id!(val)
self.wistia_project_ID = val # self is necessary here
save # or self.save, but the self is unnecessary here
end
My routes.rb:
.
.
.
resources :users do
member do
patch 'set_wistia_project_ID'
end
end
My problem is that right now, when you press the button, it says: Completed 500 Internal Server Error in 26ms (ActiveRecord: 0.7ms)
and
NoMethodError (undefined method `set_project_id' for #<User:0x000055b1a0914ab8>
2019-06-26T14:46:34.940086+00:00 app[web.1]: Did you mean? wistia_project_id):
Zavitoski got it right. I suggest, however, that you're doing a number of things more fundamentally wrong. Given that you're early in your rails journey, I hope you don't mind if I point a few things out.
First, and to be nit-picky, yes, you created a button. But, it is not a button "where users can input stuff in a field and then press the button to update the database". You created a button on a form. And you created a field on that form. The user can input stuff into the field. And when clicked, the button submits the form which includes the information in the field.
Now, on that form, you did:
<%= form_for(#user) do |f| %>
<%= f.label :wistia_project_id %>
<%= f.text_field :wistia_project_id, class: 'form-control' %>
<%= f.submit "Save", :action => "set_wistia_project_ID", :method => :patch, :form_class => "form-control" %>
<% end %>
There are a few things wrong with:
:action => "set_wistia_project_ID"
First, set_wisteria_project_ID is not a very ruby-ish action name. set_wistia_project_id would be more like it. Also, you're using old-form key-value formatting. And, you can use a symbol instead of a string for your action name so your code is prettier. Something, perhaps, like:
<%= f.submit "Save", action: :set_wistia_project_id, method: :patch, form_class: "form-control" %>
But, that's a mistake, too. Because you don't need a set_wistia_project_id action. (It's an action or a method, not a function.) You already have the update action. And form_for is smart enough to submit to this action if #user is an instance of User. So, really, you should do:
<%= form_for #user do |f| %>
<%= f.label :wistia_project_id %>
<%= f.text_field :wistia_project_id, class: 'form-control' %>
<%= f.submit "Save", form_class: "form-control" %>
<% end %>
I'm not sure what form_class is, but I'll trust that it's correct.
Now, in your UsersController, just do:
class UsersController < ApplicationController
def update
#user = User.find(params[:id])
if user.update(user_params)
# do something successful
else
# do something unsuccessful
end
end
private
def user_params
# NOTE: You'll probably want to permit other stuff here, too.
params.require(:user).permit(:wistia_project_id)
end
end
Get rid of this:
class User < ApplicationRecord
# Sets the wistia_project_ID.
def set_project_id!(val)
self.wistia_project_ID = val # self is necessary here
save # or self.save, but the self is unnecessary here
end
end
Because you're just duplicating the update method. And, you probably want that attribute to be wistia_project_id, not wistia_project_ID. (Again, you never see _ID as the suffix in rails core and you might as well be conventional.) And, if you make sure you have your association set up correctly, ActiveRecord should make sure that wistia_project_id is actually a valid value.
And write your routes.rb like this:
resources :users
Because you don't need all that set_wistia_project_id business.
It appears that you are not calling the function by the name you defined, neither passing the parameter (project_id) needed.
def set_wistia_project_ID
#user = User.find(params[:id])
#user.set_project_id!(params[:wistia_project_id])
unless #user.valid?
#errors = #user.errors.full_messages
render :show
end
end
This should use the function you created and pass the parameter from the form.
I have Challenges containing Puns, and it is possible to vote on puns. On the Challenge Show page, all puns are rendered and show their votes count. This is currently on the view page:
<%= render #challenge.puns.reverse %>
<br>
<div id="form">
<%= render "puns/form" %>
</div>
I want the puns form to appear above the items (puns) already submitted. But if swap them around, like this:
<div id="form">
<%= render "puns/form" %>
</div>
<%= render #challenge.puns.reverse %>
I get a controller error and pun.id is not suddenly not available and the voting link breaks.
No route matches {:action=>"upvote", :challenge_id=>"9", :controller=>"puns", :id=>nil}, missing required keys: [:id]
Here is the puns/form part that is causing the issue
<% if signed_in? %>
<% if current_user.voted_for? pun %>
<%= pun.votes_for.size %>
<span class="pun_text"><%= link_to pun.pun_text, challenge_pun_path(#challenge, pun.id) %></span>
<% else %>
<%= link_to like_challenge_pun_path(#challenge, pun.id), method: :put do %>
<span class="heart_like">❤</span> <%= pun.votes_for.size %>
<% end %>
<span class="pun_text"><%= link_to pun.pun_text, challenge_pun_path(#challenge, pun.id) %></span>
<% end %>
<% end %>
It is the like_challenge_pun_path that throws an error but I cannot understand why. I am declaring #challenge again here, so it should be able to get the id.
Here is the form for the puns:
<%= form_for([#challenge, #challenge.puns.build]) do |f| %>
<span class=".emoji-picker-container">
<%= f.text_area :pun_text, placeholder: "Add pun", data: { emojiable: true } %>
</span>
<%= f.submit %>
<% end %>
Also, here is my routes setup
resources :challenges do
resources :puns do
member do
put "like", to: "puns#upvote"
put "dislike", to: "puns#downvote"
end
end
end
and the corresponding action to upvote
def upvote
#pun = #challenge.puns.find(params[:id])
#pun.upvote_by current_user
redirect_to #challenge
end
Can anyone help?
I think the code is for the puns collection.
I assume the issue is that in the form you have something like:
#challenge.puns.build
So in #challenge.puns collection appears not persisted record (without id), so path for this model cannot be generated.
As a quick solution I suggest:
<%= render #challenge.puns.reverse.select(&:persisted?) %>
UPDATE:
As I assumed you have
<%= form_for([#challenge, #challenge.puns.build]) do |f| %>
You can also try:
<%= form_for([#challenge, Pun.new]) do |f| %>
Or solve it in the controller. But need to see controller code for it.
I'm a real newbie at Ruby and Rails, and I've been looking for the solution for two days. I need to submit data from form_tag to action 'create' in my controller to add new entries to database, but looks like I'm doing something terribly wrong, because absolutely nothing happens, and it seems that form_tag doesn't even redirect to needed action.
Here's the page code:
<h1>Todos</h1>
<% #projects.each do |project| %>
<tr>
<h2><%= project.title %></h2>
<% project.todos.each do |todo| %>
<ul style="list-style-type:disc">
<li><%= todo.text %></li>
</ul>
<% end %>
</tr>
<% end %>
<%= form_tag({controller: "mega", action: "create"}, method: "get", remote: true) do %>
<h2>New todo</h2>
<p>
<%= text_field_tag 'text' %>
</p>
<p>
<%= select_tag 'title', options_from_collection_for_select(#projects, 'id', 'title') %>
</p>
<p>
<%= link_to 'CANCEL' %>
<%= link_to 'OK', "", :onclick => "$('#form_id').submit()" %>
</p>
<% end %>
And the controller:
class MegaController < ApplicationController
def index
#projects = Project.all
#todos = Todo.all
end
def update
end
def create
#newTodo = Todo.create(text: params[:text])
#newProject = Project.find_by(title: params[:title])
#newProject.todos << #todo
#newTodo.save
end
end
My routes file. I seriously don't know how it works:
Rails.application.routes.draw do
get 'mega/index'
root 'mega#index'
get 'mega/update'
post 'mega/create'
resources :todos
resources :projects
end
You create resources with a POST request. Never GET.
GET requests should be idempotent - they should not update or alter resources on the server. One very important reason is that they are stored in the browser's history, so pressing the back button will cause unintended consequences for the user.
In Rails flavor MVC instead of tacking the action name on the path of the route you use the HTTP verb to create routes to the correct action:
GET /things things#index
POST /things things#create
I'm not going to attempt to salvage your code (it's deeply flawed) and instead show you how you would solve this the rails way as it is much simpler:
<%= form_for(Todo.new) do |f| %>
<h2>New todo</h2>
<%= f.text_field :text %>
<%= f.collection_select(:project_id, #projects, :id, :title, prompt: true) %>
<%= f.submit %>
<% end %>
This would submit to todos#create - if you want to route it to an unconventional action you can use the url option:
<%= form_for(Todo.new, url: polymorphic_path(controller: 'foo', action: 'bar')) do |f| %>
It's best to learn the rules before you break them.
My simple form is not doing a POST. I've been looking at this and haven't been able to see what is wrong (I'm sure it's in my routes). Here's what I have:
view:
views/buyers/new.html.erb
<%= form_for(#buyer) do |f| %>
<%= f.text_field :phone %><br />
<%= f.text_field :make %><br />
<%= f.text_field :model %><br />
<%= f.submit %>
<% end %>
controller:
BuyersController
def new
#title = "Welcome to Car Finder"
#buyer = Buyer.new
end
def create
#buyer = Buyer.new(params[:buyer])
if #buyer.save!
redirect_to success
else
redirect_to :back
end
end
routes:
resources :buyers
rake routes:
buyers GET /buyers(.:format) buyers#index
POST /buyers(.:format) buyers#create
new_buyer GET /buyers/new(.:format) buyers#new
edit_buyer GET /buyers/:id/edit(.:format) buyers#edit
buyer GET /buyers/:id(.:format) buyers#show
PUT /buyers/:id(.:format) buyers#update
DELETE /buyers/:id(.:format) buyers#destroy
When I submit the form, it stays on the same page, never going to the create action. Below is from the log
Started GET "/?..[params]..."
Processing by BuyersController#new as HTML
Thanks for any help you can give
It is probably wise to restart your server. You're issue may lie in validations you have at your persistence level or in your buyer.rb file. Add this to the _form.html.erb:
<% if #buyer.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#buyer.errors.count, "error") %> prohibited this from being saved: </h2>
<ul>
<% #buyer.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Try to complete the request again. See if any errors are being thrown. Fix those.