I am trying to change the status of an attribute called listing_status from Active to Terminated. I tried it like this and it is not working. Any idea what I am doing wrong?
<%=link_to("Terminate", listing_path(listing, :listing_status => "Terminated"), :method => :put, :confirm => "Are you sure you want to TERMINATE this listing?", :class => 'btn btn-danger')%>
Listing Controller Update Action
def update
#listing = Listing.find(params[:id])
respond_to do |format|
if #listing.update_attributes(params[:listing])
if #listing.listing_status == "Active"
#listing.sold.destroy if #listing.sold
end
flash[:notice] = 'house was successfully updated'
format.html { redirect_to :action=> "show" }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
You're passing the status as params[:listing_status], but trying to update the Listing with params[:listing]. Since there's no parameter with that name, no updates will occur.
You probably want to change your link to fit the expected parameter format:
<%= link_to("Terminate",
listings_path(listing, 'listing[listing_status]' => "Terminated"),
:method => :put, :confirm => "Are you sure you want to TERMINATE this listing?",
:class => 'btn btn-danger') %>
Related
I have a nested form (see below)
Model Artist (artist is a user)
has_many :art_works
has_many :canvases
accepts_nested_attributes_for :art_works //artworks is what im currently working on
accepts_nested_attributes_for :canvases
Controller art_works
def new
#artist = Artist.find(params[:artist_id])
#artwork = #artist.art_works.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #artwork }
end
end
def create
#artwork = ArtWork.new(params[:artwork])
respond_to do |format|
if #artwork.save
format.html { redirect_to #artwork, notice: 'artwork was successfully created.' }
format.json { render json: #artwork, status: :created, location: #artwork }
else
format.html { render action: "new" }
format.json { render json: #artwork.errors, status: :unprocessable_entity }
end
end
end
artworks views _form
<%= form_for(#artwork, :url => artist_art_works_path(current_artist) :multipart => true) do |f| %>
<p>
<%= f.file_field :art %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I was pretty positive that this would work, but i'm assuming my :url is incorrect? I'm not really sure what else it would be though. below are my routes for artworks the reason why I am nesting these things is because an artist can upload art into an artwork model the idea is to have several pieces of art in one artwork (like an album has many images inside of it)
artist_art_works GET /artists/:artist_id/art_works(.:format) art_works#index
POST /artists/:artist_id/art_works(.:format) art_works#create
new_artist_art_work GET /artists/:artist_id/art_works/new(.:format) art_works#new
edit_artist_art_work GET /artists/:artist_id/art_works/:id/edit(.:format) art_works#edit
artist_art_work GET /artists/:artist_id/art_works/:id(.:format) art_works#show
PUT /artists/:artist_id/art_works/:id(.:format) art_works#update
DELETE /artists/:artist_id/art_works/:id(.:format) art_works#destroy
Thank you very much in advance for your help. (sorry for the noobness)
You're missing a comma. Yeah the error message isn't that helpful.
#artwork, :url => artist_art_works_path(current_artist) :multipart => true
vs
#artwork, :url => artist_art_works_path(current_artist), :multipart => true
I have a form that I want a user to put in their name and email, which get stored in a session. They can then post text in a chat box.
In my view, to create the initial session:
<%= simple_form_for(#comments, :url => guest_login_order_path(#order)) do |f| %>
<input name="comment[new_user_comment_name]" />
<input name="comment[new_user_comment_email]" />
<%= f.button :submit, :value => 'Guest Signin', :class => '' %>
<% end %>
This goes to my controller:
def guest_login
#order = Order.where(:public_hash => params[:public_hash]).first
session[:new_user_account] = params[:new_user]
respond_to do |format|
if session[:new_user_account]
format.html { redirect_to #order, notice: 'Your account has been created.' }
format.json { render json: #order, status: :created, location: #order }
else
format.html { render action: "invoice" }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
The params get passed correctly but I'm not quite sure if the cookie is being created. Is there way to specify a name so I can see if it was created? Also in the view, would I have a conditional then to see if there is a session present?
Rails creates a session for you so you don't need to check if it's present. If you'd like to easily retrieve the user from the session you can create a helper method in your application controller:
class ApplicationController < ActionController::Base
def current_user
#current_user ||= session[:new_user_account]
end
helper_method :current_user
end
This method will be available to other controllers and views in your app.
I'm creating a basic loyalty card application, with model Merchant and Loyaltycard. In the merchant#show view I have this line
<%=link_to 'New Loyalty card', new_loyaltycard_path(:merchant_id=>1) %>
I'm trying to pass the merchant ID to the loyaltycard#new view so it will be automatically selected as the merchant for that card. In loyaltycard#_form (which gets called by loyaltycard#new) I have the lines
<%if #merchant%>
<%= f.hidden_field :merchant_id, :value => #merchant.id %>
<%else%>
<div class="field">
<%= f.label :merchant_id %><br />
<%= f.text_field :merchant_id %>
</div>
<%end%>
But I keep getting and error that says can't call id for class Nil. Is there a better way of doing this?
Here is the controller code for loyaltycard
def new
#loyaltycard = Loyaltycard.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #loyaltycard }
end
end
# GET /loyaltycards/1/edit
def edit
#loyaltycard = Loyaltycard.find(params[:id])
end
# POST /loyaltycards
# POST /loyaltycards.json
def create
#loyaltycard = Loyaltycard.new(params[:loyaltycard])
respond_to do |format|
if #loyaltycard.save
format.html { redirect_to #loyaltycard, notice: 'Loyaltycard was successfully created.' }
format.json { render json: #loyaltycard, status: :created, location: #loyaltycard }
else
format.html { render action: "new" }
format.json { render json: #loyaltycard.errors, status: :unprocessable_entity }
end
end
end
The error is
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
What you need to do in your new action is using the params[:merchant_id] to look up and set #merchant
#merchant = Merchant.find(params[:merchant_id])
Then your code should work, without that, #merchant is nil, and you can't call the method :id on nil
You're not setting variable #merchant anywhere in your controller, but you use it the view.
I have a form to update an item called post, but i need a different method than update because there are 2 ways to update the post, i have tried this in a lot of ways but i only get this error
No route matches "/topics/1/posts/35/completed"
the controller:
def completed
#post.download_remote_image
respond_to do |format|
if #post.save
format.html { redirect_to topic_path(#topic), :notice => t('.post_created') }
else
format.html { render :action => :edit }
end
end
end
the view part:
= form_for [#topic, #post], :url => completed_topic_post_path(#topic, #post) do |f|
the routes:
resources :topics do
resources :posts do
get 'complete', :as => :complete
post 'completed', :as => :completed
end
end
Thanks !!
:url => completed_topic_post_path(#topic, #post) needs to be :url => topic_post_completed_path(#topic, #post)
HI Guys. I wanna know how can I make a simple search using named scope in rails 3. I have successfully done it in the console but I can't find any example that uses the views.
Here is the code for the model trap.rb:
class Trap < ActiveRecord::Base
scope :by_date_entry, lambda { |arg| where(["traps.date_entry = ?",arg])}
scope :by_empcode, lambda { |arg| where(["traps.empcode = ?",arg])}
end
In the controller traps_controller.rb:
class TrapsController < ApplicationController
# GET /traps
# GET /traps.xml
def index
#traps = Trap.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #traps }
end
end
# GET /traps/1
# GET /traps/1.xml
def show
#trap = Trap.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #trap }
end
end
# GET /traps/new
# GET /traps/new.xml
def new
#trap = Trap.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #trap }
end
end
# GET /traps/1/edit
def edit
#trap = Trap.find(params[:id])
end
# POST /traps
# POST /traps.xml
def create
#trap = Trap.new(params[:trap])
respond_to do |format|
if #trap.save
format.html { redirect_to(#trap, :notice => 'Trap was successfully created.') }
format.xml { render :xml => #trap, :status => :created, :location => #trap }
else
format.html { render :action => "new" }
format.xml { render :xml => #trap.errors, :status => :unprocessable_entity }
end
end
end
# PUT /traps/1
# PUT /traps/1.xml
def update
#trap = Trap.find(params[:id])
respond_to do |format|
if #trap.update_attributes(params[:trap])
format.html { redirect_to(#trap, :notice => 'Trap was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #trap.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /traps/1
# DELETE /traps/1.xml
def destroy
#trap = Trap.find(params[:id])
#trap.destroy
respond_to do |format|
format.html { redirect_to(traps_url) }
format.xml { head :ok }
end
end
end
And in my view index.html.erb:
<h2>Time Reconciliation Application for Payroll 1.0</h2>
<table>
<tr>
<th>Empcode</th>
<th>Date entry</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #traps.each do |trap| %>
<tr>
<td><%= trap.empcode %></td>
<td><%= trap.date_entry %></td>
<td><%= link_to 'Show', trap %></td>
<td><%= link_to 'Edit', edit_trap_path(trap) %></td>
<td><%= link_to 'Destroy', trap, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Trap', new_trap_path %>
I wanna put like a search text box in which the user can input the employee_code or the date_entry in the index.html.erb. I have done something like this using meta_search and meta_where but I would prefer to use the named_scope but I don't know how to display this using view and controller. The codes in my model trap.rb is working on my console. I just don't know how to make it appear in the view. Pls help...
I would probably do something like this in the controller:
def index
# the scoped method returns a prepared database call with
# no arguments so the database is not called yet.
#traps = Trap.scoped
# if any parameter to filter is supplied then use the scope
#traps = #traps.by_date_entry(params[:date_entry]) if params[:date_entry]
#traps = #traps.by_empcode(params[:empcode]) if params[:empcode]
end
And then in the view, you could either go with a form to send the parameters, or if you just want to try this then create a link for the specific dates or empcodes like this:
<td><%= link_to trap.date_entry, traps_path(:date_entry => trap.date_entry) %></td>
So that when you click the link it will send the date_entry to be used by your scope. By the way, the traps_path is of course only valid as long as you specified resources :traps in your routes.rb