如何告诉rails在哪里查找特定模板?

时间:2012-11-05 05:07:48

标签: ruby-on-rails-3 paperclip

我有一个名为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

编辑:

这是userscontroller的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>

1 个答案:

答案 0 :(得分:0)

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