0

我有一个名为 Purchase.rb 的模型。每次购买都是通过如下表格创建的。

<%= form_for(@purchase) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :content, placeholder: "Describe something you are interested in buying.", :maxlength=>"254" %>
    <%= f.file_field :photo %>

  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

当它们被创建时,它们的显示部分存储在 app/views/purchases/_purchase.html.erb 中。要添加照片,我已经放

<%= form_for(@purchase) do |f| %>
    <%= f.file_field :photo %>
    <%= f.submit "Post", class: "btn btn-large btn-primary" %>
  <% end %>

在购买本身。我正在使用回形针。所以这个想法是人们可以点击作为购买一部分的字段并将照片添加到视图中。

我得到的错误说

Missing template purchases/users#show, application/users#show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/alex/rails_projects/tradespring!/app/views"

我希望它查看 app/views/users#show,而不是 app/views/purchases/users#show

编辑:

这是用户控制器的显示操作:

class UsersController < ApplicationController


  def show
    @user = User.find(params[:id])
    @purchases= @user.purchases
    @sales= @user.sales
    @purchase=Purchase.new
    @sale=Sale.new
  end

这是 routes.rb

Tradespring::Application.routes.draw do
  resources :users do
    resources :pcomments
    resources :scomments
  end
  resources :sessions, only: [:new, :create, :destroy]
  resources :purchases do
    resources :pcomments
  end
  resources :sales do
    resources :scomments
  end

  get "static_pages/home"

  get "static_pages/about"

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new' 
  match '/about',   to: 'static_pages#about'
  match '/signout', to: 'sessions#destroy', via: :delete

  root to: 'static_pages#home'

最后,这是我提交图片表单时想要发生的事情。我不是 100% 确定它应该在更新中。

class PurchasesController < ApplicationController
 def update
    @purchase = Purchase.find(params[:id])
    if @purchase.update_attributes(params[:purchase])
      flash[:success] = "Picture added"

      redirect_to :back
    else
      render 'users/show'
    end
  end

这里也是 app/views/user/show.html.erb

<% provide(:title, @user.name) %>
<p>
<%= mail_to(@user.email, name="email this user", :encode => "javascript") %>
</p>

<div id="purchases">

<%= form_for(@purchase) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :content, placeholder: "Describe something you are interested in buying.", :maxlength=>"254" %>
    <%= f.file_field :photo %>
    <p1> Note: There is a 254 character limit. Be sure to include useful information such as product specifications, how much you are willing to pay, and shipping info (where you live, if you want to pick it up locally, ect.). Further detail is best left to email.</p1>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

<% if @user.purchases.any? %>
      <h3>Purchases (<%= @user.purchases.count %>)</h3>
      <ol class="purchases">
        <%= render @purchases %>
      </ol>
<% end %>
</div>


<div id="sales">

<%= form_for(@sale) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :content, placeholder: "Describe something you are interested in selling.", :maxlength=>"254" %>
    <p1> Note: There is a 254 character limit. Be sure to include useful information such as product specifications, price, payment methods accepted, and shipping info (where you live, if you are willing to ship it, ect.). Further detail is best left to email. </p1>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

<% if @user.sales.any? %>
      <h3>Sales (<%= @user.sales.count %>)</h3>
      <ol class="sales">
        <%= render @sales %>
      </ol>
<% end %>
</div>
4

1 回答 1

0

问题是我需要定义第二种形式的动作。我猜默认情况下,表单假定您想要创建操作。在这种情况下,我希望它使用更新操作,因此我需要将其更改为 <%= form_for((@purchase), :url => { :action => "update" }) do |f| %>

于 2012-11-08T09:20:31.667 回答